3 #include <simgear/package/Root.hxx>
5 #include <boost/foreach.hpp>
8 #include <simgear/debug/logstream.hxx>
9 #include <simgear/props/props_io.hxx>
10 #include <simgear/io/HTTPRequest.hxx>
11 #include <simgear/io/HTTPClient.hxx>
12 #include <simgear/misc/sg_dir.hxx>
13 #include <simgear/structure/exception.hxx>
14 #include <simgear/package/Package.hxx>
15 #include <simgear/package/Install.hxx>
16 #include <simgear/package/Catalog.hxx>
22 void Root::setMaxAgeSeconds(int seconds)
24 m_maxAgeSeconds = seconds;
27 void Root::setHTTPClient(HTTP::Client* aHTTP)
32 HTTP::Client* Root::getHTTPClient() const
37 Root::Root(const SGPath& aPath) :
40 m_maxAgeSeconds(60 * 60 * 24),
43 if (getenv("LOCALE")) {
44 m_locale = getenv("LOCALE");
53 BOOST_FOREACH(SGPath c, d.children(Dir::TYPE_DIR)) {
54 Catalog* cat = Catalog::createFromPath(this, c);
56 m_catalogs[cat->id()] = cat;
58 } // of child directories iteration
66 Catalog* Root::getCatalogById(const std::string& aId) const
68 CatalogDict::const_iterator it = m_catalogs.find(aId);
69 if (it == m_catalogs.end()) {
76 Package* Root::getPackageById(const std::string& aName) const
78 size_t lastDot = aName.rfind('.');
81 if (lastDot == std::string::npos) {
83 CatalogDict::const_iterator it = m_catalogs.begin();
84 for (; it != m_catalogs.end(); ++it) {
85 pkg = it->second->getPackageById(aName);
94 std::string catalogId = aName.substr(0, lastDot);
95 std::string id = aName.substr(lastDot + 1);
96 Catalog* catalog = getCatalogById(catalogId);
101 return catalog->getPackageById(id);
104 CatalogList Root::catalogs() const
107 CatalogDict::const_iterator it = m_catalogs.begin();
108 for (; it != m_catalogs.end(); ++it) {
109 r.push_back(it->second);
116 Root::packagesMatching(const SGPropertyNode* aFilter) const
120 CatalogDict::const_iterator it = m_catalogs.begin();
121 for (; it != m_catalogs.end(); ++it) {
122 PackageList r2(it->second->packagesMatching(aFilter));
123 r.insert(r.end(), r2.begin(), r2.end());
130 Root::packagesNeedingUpdate() const
134 CatalogDict::const_iterator it = m_catalogs.begin();
135 for (; it != m_catalogs.end(); ++it) {
136 PackageList r2(it->second->packagesNeedingUpdate());
137 r.insert(r.end(), r2.begin(), r2.end());
143 void Root::refresh(bool aForce)
145 CatalogDict::iterator it = m_catalogs.begin();
146 for (; it != m_catalogs.end(); ++it) {
147 if (aForce || (it->second->ageInSeconds() > m_maxAgeSeconds)) {
148 it->second->refresh();
153 void Root::setLocale(const std::string& aLocale)
158 std::string Root::getLocale() const
163 void Root::scheduleToUpdate(Install* aInstall)
166 sg_exception("missing argument to scheduleToUpdate");
169 PackageList deps = aInstall->package()->dependencies();
170 BOOST_FOREACH(Package* dep, deps) {
171 // will internally schedule for update if required
172 // hence be careful, this method is re-entered in here!
176 bool wasEmpty = m_updateDeque.empty();
177 m_updateDeque.push_back(aInstall);
179 aInstall->startUpdate();
183 void Root::startInstall(Install* aInstall)
186 m_delegate->startInstall(aInstall);
190 void Root::installProgress(Install* aInstall, unsigned int aBytes, unsigned int aTotal)
193 m_delegate->installProgress(aInstall, aBytes, aTotal);
197 void Root::startNext(Install* aCurrent)
199 if (m_updateDeque.front() != aCurrent) {
200 SG_LOG(SG_GENERAL, SG_ALERT, "current install of package not head of the deque");
202 m_updateDeque.pop_front();
205 if (!m_updateDeque.empty()) {
206 m_updateDeque.front()->startUpdate();
210 void Root::finishInstall(Install* aInstall)
213 m_delegate->finishInstall(aInstall);
219 void Root::failedInstall(Install* aInstall, Delegate::FailureCode aReason)
221 SG_LOG(SG_GENERAL, SG_ALERT, "failed to install package:"
222 << aInstall->package()->id() << ":" << aReason);
224 m_delegate->failedInstall(aInstall, aReason);
230 void Root::catalogRefreshBegin(Catalog* aCat)
232 m_refreshing.insert(aCat);
235 void Root::catalogRefreshComplete(Catalog* aCat, bool aSuccess)
237 m_refreshing.erase(aCat);
238 if (m_refreshing.empty()) {
240 m_delegate->refreshComplete();
245 } // of namespace pkg
247 } // of namespace simgear