1 // Copyright (C) 2013 James Turner - zakalawe@mac.com
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.
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.
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.
18 #include <simgear/package/Root.hxx>
20 #include <boost/foreach.hpp>
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>
37 void Root::setMaxAgeSeconds(int seconds)
39 m_maxAgeSeconds = seconds;
42 void Root::setHTTPClient(HTTP::Client* aHTTP)
47 HTTP::Client* Root::getHTTPClient() const
52 Root::Root(const SGPath& aPath, const std::string& aVersion) :
55 m_maxAgeSeconds(60 * 60 * 24),
59 if (getenv("LOCALE")) {
60 m_locale = getenv("LOCALE");
69 BOOST_FOREACH(SGPath c, d.children(Dir::TYPE_DIR)) {
70 Catalog* cat = Catalog::createFromPath(this, c);
72 m_catalogs[cat->id()] = cat;
74 } // of child directories iteration
82 std::string Root::catalogVersion() const
87 Catalog* Root::getCatalogById(const std::string& aId) const
89 CatalogDict::const_iterator it = m_catalogs.find(aId);
90 if (it == m_catalogs.end()) {
97 Package* Root::getPackageById(const std::string& aName) const
99 size_t lastDot = aName.rfind('.');
102 if (lastDot == std::string::npos) {
104 CatalogDict::const_iterator it = m_catalogs.begin();
105 for (; it != m_catalogs.end(); ++it) {
106 pkg = it->second->getPackageById(aName);
115 std::string catalogId = aName.substr(0, lastDot);
116 std::string id = aName.substr(lastDot + 1);
117 Catalog* catalog = getCatalogById(catalogId);
122 return catalog->getPackageById(id);
125 CatalogList Root::catalogs() const
128 CatalogDict::const_iterator it = m_catalogs.begin();
129 for (; it != m_catalogs.end(); ++it) {
130 r.push_back(it->second);
137 Root::packagesMatching(const SGPropertyNode* aFilter) const
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());
151 Root::packagesNeedingUpdate() const
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());
164 void Root::refresh(bool aForce)
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();
174 void Root::setDelegate(simgear::pkg::Delegate *aDelegate)
176 m_delegate = aDelegate;
179 void Root::setLocale(const std::string& aLocale)
184 std::string Root::getLocale() const
189 void Root::scheduleToUpdate(Install* aInstall)
192 sg_exception("missing argument to scheduleToUpdate");
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!
202 bool wasEmpty = m_updateDeque.empty();
203 m_updateDeque.push_back(aInstall);
206 aInstall->startUpdate();
210 void Root::startInstall(Install* aInstall)
213 m_delegate->startInstall(aInstall);
217 void Root::installProgress(Install* aInstall, unsigned int aBytes, unsigned int aTotal)
220 m_delegate->installProgress(aInstall, aBytes, aTotal);
224 void Root::startNext(Install* aCurrent)
226 if (m_updateDeque.front() != aCurrent) {
227 SG_LOG(SG_GENERAL, SG_ALERT, "current install of package not head of the deque");
229 m_updateDeque.pop_front();
232 if (!m_updateDeque.empty()) {
233 m_updateDeque.front()->startUpdate();
237 void Root::finishInstall(Install* aInstall)
240 m_delegate->finishInstall(aInstall);
246 void Root::failedInstall(Install* aInstall, Delegate::FailureCode aReason)
248 SG_LOG(SG_GENERAL, SG_ALERT, "failed to install package:"
249 << aInstall->package()->id() << ":" << aReason);
251 m_delegate->failedInstall(aInstall, aReason);
257 void Root::catalogRefreshBegin(Catalog* aCat)
259 m_refreshing.insert(aCat);
262 void Root::catalogRefreshComplete(Catalog* aCat, Delegate::FailureCode aReason)
264 CatalogDict::iterator catIt = m_catalogs.find(aCat->id());
265 if (aReason != Delegate::FAIL_SUCCESS) {
267 m_delegate->failedRefresh(aCat, aReason);
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);
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));
282 m_refreshing.erase(aCat);
283 if (m_refreshing.empty()) {
285 m_delegate->refreshComplete();
290 } // of namespace pkg
292 } // of namespace simgear