]> git.mxchange.org Git - simgear.git/blob - simgear/package/Root.cxx
Catalog install feedback.
[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 #include <map>
23 #include <deque>
24 #include <set>
25
26 #include <simgear/debug/logstream.hxx>
27 #include <simgear/props/props_io.hxx>
28 #include <simgear/io/HTTPRequest.hxx>
29 #include <simgear/io/HTTPClient.hxx>
30 #include <simgear/misc/sg_dir.hxx>
31 #include <simgear/structure/exception.hxx>
32 #include <simgear/package/Package.hxx>
33 #include <simgear/package/Install.hxx>
34 #include <simgear/package/Catalog.hxx>
35
36 namespace simgear {
37     
38 namespace pkg {
39
40 typedef std::map<std::string, CatalogRef> CatalogDict;
41     
42 class Root::RootPrivate
43 {
44 public:
45     RootPrivate() :
46         http(NULL),
47         maxAgeSeconds(60 * 60 * 24),
48         delegate(NULL)
49     {
50         
51     }
52     
53     SGPath path;
54     std::string locale;
55     HTTP::Client* http;
56     CatalogDict catalogs;
57     unsigned int maxAgeSeconds;
58     Delegate* delegate;
59     std::string version;
60     
61     std::set<CatalogRef> refreshing;
62     std::deque<InstallRef> updateDeque;
63     std::deque<HTTP::Request_ptr> httpPendingRequests;
64 };
65     
66 SGPath Root::path() const
67 {
68     return d->path;
69 }
70     
71 void Root::setMaxAgeSeconds(unsigned int seconds)
72 {
73     d->maxAgeSeconds = seconds;
74 }
75     
76 unsigned int Root::maxAgeSeconds() const
77 {
78     return d->maxAgeSeconds;
79 }
80
81 void Root::setHTTPClient(HTTP::Client* aHTTP)
82 {
83     d->http = aHTTP;
84     BOOST_FOREACH(HTTP::Request_ptr req, d->httpPendingRequests) {
85         d->http->makeRequest(req);
86     }
87
88     d->httpPendingRequests.clear();
89 }
90
91 void Root::makeHTTPRequest(HTTP::Request *req)
92 {
93     if (d->http) {
94         d->http->makeRequest(req);
95         return;
96     }
97     
98     d->httpPendingRequests.push_back(req);
99 }
100     
101 Root::Root(const SGPath& aPath, const std::string& aVersion) :
102     d(new RootPrivate)
103 {
104     d->path = aPath;
105     d->version = aVersion;
106     if (getenv("LOCALE")) {
107         d->locale = getenv("LOCALE");
108     }
109     
110     Dir dir(aPath);
111     if (!dir.exists()) {
112         dir.create(0755);
113         return;
114     }
115     
116     BOOST_FOREACH(SGPath c, dir.children(Dir::TYPE_DIR)) {
117         CatalogRef cat = Catalog::createFromPath(this, c);
118         if (cat) {
119            d->catalogs[cat->id()] = cat;
120         }
121     } // of child directories iteration
122 }
123
124 Root::~Root()
125 {
126     
127 }
128     
129 std::string Root::catalogVersion() const
130 {
131     return d->version;
132 }
133     
134 CatalogRef Root::getCatalogById(const std::string& aId) const
135 {
136     CatalogDict::const_iterator it = d->catalogs.find(aId);
137     if (it == d->catalogs.end()) {
138         return NULL;
139     }
140     
141     return it->second;
142 }
143
144 PackageRef Root::getPackageById(const std::string& aName) const
145 {
146     size_t lastDot = aName.rfind('.');
147     
148     PackageRef pkg = NULL;
149     if (lastDot == std::string::npos) {
150         // naked package ID
151         CatalogDict::const_iterator it = d->catalogs.begin();
152         for (; it != d->catalogs.end(); ++it) {
153             pkg = it->second->getPackageById(aName);
154             if (pkg) {
155                 return pkg;
156             }
157         }
158         
159         return NULL;
160     }
161     
162     std::string catalogId = aName.substr(0, lastDot);
163     std::string id = aName.substr(lastDot + 1);    
164     CatalogRef catalog = getCatalogById(catalogId);
165     if (!catalog) {
166         return NULL;
167     }
168             
169     return catalog->getPackageById(id);
170 }
171
172 CatalogList Root::catalogs() const
173 {
174     CatalogList r;
175     CatalogDict::const_iterator it = d->catalogs.begin();
176     for (; it != d->catalogs.end(); ++it) {
177         r.push_back(it->second);
178     }
179     
180     return r;
181 }
182
183 PackageList
184 Root::packagesMatching(const SGPropertyNode* aFilter) const
185 {
186     PackageList r;
187     
188     CatalogDict::const_iterator it = d->catalogs.begin();
189     for (; it != d->catalogs.end(); ++it) {
190         PackageList r2(it->second->packagesMatching(aFilter));
191         r.insert(r.end(), r2.begin(), r2.end());
192     }
193     
194     return r;
195 }
196
197 PackageList
198 Root::packagesNeedingUpdate() const
199 {
200     PackageList r;
201     
202     CatalogDict::const_iterator it = d->catalogs.begin();
203     for (; it != d->catalogs.end(); ++it) {
204         PackageList r2(it->second->packagesNeedingUpdate());
205         r.insert(r.end(), r2.begin(), r2.end());
206     }
207     
208     return r;
209 }
210
211 void Root::refresh(bool aForce)
212 {
213     CatalogDict::iterator it = d->catalogs.begin();
214     for (; it != d->catalogs.end(); ++it) {
215         if (aForce || it->second->needsRefresh()) {
216             it->second->refresh();
217         }
218     }
219 }
220
221 void Root::setDelegate(simgear::pkg::Delegate *aDelegate)
222 {
223     d->delegate = aDelegate;
224 }
225     
226 void Root::setLocale(const std::string& aLocale)
227 {
228     d->locale = aLocale;
229 }
230
231 std::string Root::getLocale() const
232 {
233     return d->locale;
234 }
235
236 void Root::scheduleToUpdate(InstallRef aInstall)
237 {
238     if (!aInstall) {
239         throw sg_exception("missing argument to scheduleToUpdate");
240     }
241     
242     PackageList deps = aInstall->package()->dependencies();
243     BOOST_FOREACH(Package* dep, deps) {
244         // will internally schedule for update if required
245         // hence be careful, this method is re-entered in here!
246         dep->install();
247     }
248
249     bool wasEmpty = d->updateDeque.empty();
250     d->updateDeque.push_back(aInstall);
251     
252     if (wasEmpty) {
253         aInstall->startUpdate();
254     }
255 }
256
257 void Root::startInstall(InstallRef aInstall)
258 {
259     if (d->delegate) {
260         d->delegate->startInstall(aInstall.ptr());
261     }
262 }
263
264 void Root::installProgress(InstallRef aInstall, unsigned int aBytes, unsigned int aTotal)
265 {
266     if (d->delegate) {
267         d->delegate->installProgress(aInstall.ptr(), aBytes, aTotal);
268     }
269 }
270
271 void Root::startNext(InstallRef aCurrent)
272 {
273     if (d->updateDeque.front() != aCurrent) {
274         SG_LOG(SG_GENERAL, SG_ALERT, "current install of package not head of the deque");
275     } else {
276         d->updateDeque.pop_front();
277     }
278     
279     if (!d->updateDeque.empty()) {
280         d->updateDeque.front()->startUpdate();
281     }
282 }
283
284 void Root::finishInstall(InstallRef aInstall)
285 {
286     if (d->delegate) {
287         d->delegate->finishInstall(aInstall.ptr());
288     }
289     
290     startNext(aInstall);
291 }
292
293 void Root::failedInstall(InstallRef aInstall, Delegate::FailureCode aReason)
294 {
295     SG_LOG(SG_GENERAL, SG_ALERT, "failed to install package:" 
296         << aInstall->package()->id() << ":" << aReason);
297     if (d->delegate) {
298         d->delegate->failedInstall(aInstall.ptr(), aReason);
299     }
300     
301     startNext(aInstall);
302 }
303
304 void Root::catalogRefreshBegin(CatalogRef aCat)
305 {
306     d->refreshing.insert(aCat);
307 }
308
309 void Root::catalogRefreshComplete(CatalogRef aCat, Delegate::FailureCode aReason)
310 {
311     CatalogDict::iterator catIt = d->catalogs.find(aCat->id());
312     if (aReason != Delegate::FAIL_SUCCESS) {
313         if (d->delegate) {
314             d->delegate->failedRefresh(aCat, aReason);
315         }
316         
317         // if the failure is permanent, delete the catalog from our
318         // list (don't touch it on disk)
319         bool isPermanentFailure = (aReason == Delegate::FAIL_VERSION);
320         if (isPermanentFailure) {
321             SG_LOG(SG_GENERAL, SG_WARN, "permanent failure for catalog:" << aCat->id());
322             if (catIt != d->catalogs.end()) {
323                 d->catalogs.erase(catIt);
324             }
325         }
326     } else if (catIt == d->catalogs.end()) {
327         // first fresh, add to our storage now
328         d->catalogs.insert(catIt, CatalogDict::value_type(aCat->id(), aCat));
329     }
330     
331     d->refreshing.erase(aCat);
332     if (d->refreshing.empty()) {
333         if (d->delegate) {
334             d->delegate->refreshComplete();
335         }
336     }
337 }
338
339 bool Root::removeCatalogById(const std::string& aId)
340 {
341     CatalogDict::iterator catIt = d->catalogs.find(aId);
342     if (catIt == d->catalogs.end()) {
343         SG_LOG(SG_GENERAL, SG_WARN, "removeCatalogById: unknown ID:" << aId);
344         return false;
345     }
346     
347     CatalogRef cat = catIt->second;
348     
349     // drop the reference
350     d->catalogs.erase(catIt);
351     
352     bool ok = cat->uninstall();
353     if (!ok) {
354         SG_LOG(SG_GENERAL, SG_WARN, "removeCatalogById: catalog :" << aId
355             << "failed to uninstall");
356     }
357     
358     return ok;
359 }
360
361 } // of namespace pkg
362
363 } // of namespace simgear