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/Catalog.hxx>
20 #include <boost/foreach.hpp>
25 #include <simgear/debug/logstream.hxx>
26 #include <simgear/props/props_io.hxx>
27 #include <simgear/io/HTTPRequest.hxx>
28 #include <simgear/io/HTTPClient.hxx>
29 #include <simgear/misc/sg_dir.hxx>
30 #include <simgear/structure/exception.hxx>
31 #include <simgear/package/Package.hxx>
32 #include <simgear/package/Root.hxx>
33 #include <simgear/package/Install.hxx>
39 CatalogList static_catalogs;
41 //////////////////////////////////////////////////////////////////////////////
43 class Catalog::Downloader : public HTTP::Request
46 Downloader(Catalog* aOwner, const std::string& aUrl) :
53 virtual void gotBodyData(const char* s, int n)
55 m_buffer += std::string(s, n);
60 if (responseCode() != 200) {
61 SG_LOG(SG_GENERAL, SG_ALERT, "catalog download failure:" << m_owner->url());
62 m_owner->refreshComplete(Delegate::FAIL_DOWNLOAD);
66 SGPropertyNode* props = new SGPropertyNode;
69 readProperties(m_buffer.data(), m_buffer.size(), props);
70 m_owner->parseProps(props);
71 } catch (sg_exception& e) {
72 SG_LOG(SG_GENERAL, SG_ALERT, "catalog parse failure:" << m_owner->url());
73 m_owner->refreshComplete(Delegate::FAIL_EXTRACT);
77 std::string ver(m_owner->root()->catalogVersion());
78 if (!checkVersion(ver, props)) {
79 SG_LOG(SG_GENERAL, SG_WARN, "downloaded catalog " << m_owner->url() << ", version mismatch:\n\t"
80 << props->getStringValue("version") << " vs required " << ver);
81 m_owner->refreshComplete(Delegate::FAIL_VERSION);
85 // cache the catalog data, now we have a valid install root
86 Dir d(m_owner->installRoot());
87 SGPath p = d.file("catalog.xml");
89 std::ofstream f(p.c_str(), std::ios::out | std::ios::trunc);
90 f.write(m_buffer.data(), m_buffer.size());
93 time(&m_owner->m_retrievedTime);
94 m_owner->writeTimestamp();
95 m_owner->refreshComplete(Delegate::FAIL_SUCCESS);
99 bool checkVersion(const std::string& aVersion, SGPropertyNode* aProps)
101 BOOST_FOREACH(SGPropertyNode* v, aProps->getChildren("version")) {
102 if (v->getStringValue() == aVersion) {
110 std::string m_buffer;
113 //////////////////////////////////////////////////////////////////////////////
115 CatalogList Catalog::allCatalogs()
117 return static_catalogs;
120 Catalog::Catalog(Root *aRoot) :
124 static_catalogs.push_back(this);
129 CatalogList::iterator it = std::find(static_catalogs.begin(), static_catalogs.end(), this);
130 static_catalogs.erase(it);
133 Catalog* Catalog::createFromUrl(Root* aRoot, const std::string& aUrl)
135 Catalog* c = new Catalog(aRoot);
136 Downloader* dl = new Downloader(c, aUrl);
137 aRoot->makeHTTPRequest(dl);
142 Catalog* Catalog::createFromPath(Root* aRoot, const SGPath& aPath)
145 xml.append("catalog.xml");
150 SGPropertyNode_ptr props;
152 props = new SGPropertyNode;
153 readProperties(xml.str(), props);
154 } catch (sg_exception& e) {
158 if (props->getStringValue("version") != aRoot->catalogVersion()) {
159 SG_LOG(SG_GENERAL, SG_WARN, "skipping catalog at " << aPath << ", version mismatch:\n\t"
160 << props->getStringValue("version") << " vs required " << aRoot->catalogVersion());
164 Catalog* c = new Catalog(aRoot);
165 c->m_installRoot = aPath;
166 c->parseProps(props);
173 Catalog::packagesMatching(const SGPropertyNode* aFilter) const
176 BOOST_FOREACH(Package* p, m_packages) {
177 if (p->matches(aFilter)) {
185 Catalog::packagesNeedingUpdate() const
188 BOOST_FOREACH(Package* p, m_packages) {
189 if (!p->isInstalled()) {
193 if (p->install()->hasUpdate()) {
200 void Catalog::refresh()
202 Downloader* dl = new Downloader(this, url());
203 m_root->makeHTTPRequest(dl);
204 m_root->catalogRefreshBegin(this);
207 void Catalog::parseProps(const SGPropertyNode* aProps)
209 // copy everything except package children?
210 m_props = new SGPropertyNode;
212 int nChildren = aProps->nChildren();
213 for (int i = 0; i < nChildren; i++) {
214 const SGPropertyNode* pkgProps = aProps->getChild(i);
215 if (strcmp(pkgProps->getName(), "package") == 0) {
216 Package* p = new Package(pkgProps, this);
217 m_packages.push_back(p);
219 SGPropertyNode* c = m_props->getChild(pkgProps->getName(), pkgProps->getIndex(), true);
220 copyProperties(pkgProps, c);
222 } // of children iteration
224 if (m_installRoot.isNull()) {
225 m_installRoot = m_root->path();
226 m_installRoot.append(id());
228 Dir d(m_installRoot);
233 Package* Catalog::getPackageById(const std::string& aId) const
235 BOOST_FOREACH(Package* p, m_packages) {
236 if (p->id() == aId) {
241 return NULL; // not found
244 std::string Catalog::id() const
246 return m_props->getStringValue("id");
249 std::string Catalog::url() const
251 return m_props->getStringValue("url");
254 std::string Catalog::description() const
256 return getLocalisedString(m_props, "description");
259 SGPropertyNode* Catalog::properties() const
261 return m_props.ptr();
264 void Catalog::parseTimestamp()
266 SGPath timestampFile = m_installRoot;
267 timestampFile.append(".timestamp");
268 std::ifstream f(timestampFile.c_str(), std::ios::in);
269 f >> m_retrievedTime;
272 void Catalog::writeTimestamp()
274 SGPath timestampFile = m_installRoot;
275 timestampFile.append(".timestamp");
276 std::ofstream f(timestampFile.c_str(), std::ios::out | std::ios::trunc);
277 f << m_retrievedTime << std::endl;
280 unsigned int Catalog::ageInSeconds() const
284 int diff = ::difftime(now, m_retrievedTime);
285 return (diff < 0) ? 0 : diff;
288 bool Catalog::needsRefresh() const
290 unsigned int maxAge = m_props->getIntValue("max-age-sec", m_root->maxAgeSeconds());
291 return (ageInSeconds() > maxAge);
294 std::string Catalog::getLocalisedString(const SGPropertyNode* aRoot, const char* aName) const
296 if (aRoot->hasChild(m_root->getLocale())) {
297 const SGPropertyNode* localeRoot = aRoot->getChild(m_root->getLocale().c_str());
298 if (localeRoot->hasChild(aName)) {
299 return localeRoot->getStringValue(aName);
303 return aRoot->getStringValue(aName);
306 void Catalog::refreshComplete(Delegate::FailureCode aReason)
308 m_root->catalogRefreshComplete(this, aReason);
312 } // of namespace pkg
314 } // of namespace simgear