]> git.mxchange.org Git - simgear.git/blob - simgear/package/Catalog.cxx
70f4cc13a248106b59cce9aed6f6ef48d805df49
[simgear.git] / simgear / package / Catalog.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/Catalog.hxx>
19
20 #include <boost/foreach.hpp>
21 #include <algorithm>
22 #include <fstream>
23 #include <cstring>
24
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>
34
35 namespace simgear {
36     
37 namespace pkg {
38
39 CatalogList static_catalogs;
40
41 //////////////////////////////////////////////////////////////////////////////
42
43 class Catalog::Downloader : public HTTP::Request
44 {
45 public:
46     Downloader(CatalogRef aOwner, const std::string& aUrl) :
47         HTTP::Request(aUrl),
48         m_owner(aOwner)
49     {        
50     }
51     
52 protected:
53     virtual void gotBodyData(const char* s, int n)
54     {
55         m_buffer += std::string(s, n);
56     }
57     
58     virtual void onDone()
59     {        
60         if (responseCode() != 200) {
61             SG_LOG(SG_GENERAL, SG_ALERT, "catalog download failure:" << m_owner->url());
62             m_owner->refreshComplete(Delegate::FAIL_DOWNLOAD);
63             return;
64         }
65         
66         SGPropertyNode* props = new SGPropertyNode;
67         
68         try {
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);
74             return;
75         }
76         
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);
82             return;
83         }
84         
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");
88
89         std::ofstream f(p.c_str(), std::ios::out | std::ios::trunc);
90         f.write(m_buffer.data(), m_buffer.size());
91         f.close();
92         
93         time(&m_owner->m_retrievedTime);
94         m_owner->writeTimestamp();
95         m_owner->refreshComplete(Delegate::FAIL_SUCCESS);
96     }
97     
98 private:
99     bool checkVersion(const std::string& aVersion, SGPropertyNode* aProps)
100     {
101         BOOST_FOREACH(SGPropertyNode* v, aProps->getChildren("version")) {
102             if (v->getStringValue() == aVersion) {
103                 return true;
104             }
105         }
106         return false;
107     }
108     
109     CatalogRef m_owner;
110     std::string m_buffer;
111 };
112
113 //////////////////////////////////////////////////////////////////////////////
114
115 CatalogList Catalog::allCatalogs()
116 {
117     return static_catalogs;
118 }
119
120 Catalog::Catalog(Root *aRoot) :
121     m_root(aRoot),
122     m_retrievedTime(0)
123 {
124     static_catalogs.push_back(this);
125 }
126
127 Catalog::~Catalog()
128 {
129     CatalogList::iterator it = std::find(static_catalogs.begin(), static_catalogs.end(), this);
130     static_catalogs.erase(it);
131 }
132
133 CatalogRef Catalog::createFromUrl(Root* aRoot, const std::string& aUrl)
134 {
135     CatalogRef c = new Catalog(aRoot);
136     c->m_url = aUrl;
137     Downloader* dl = new Downloader(c, aUrl);
138     aRoot->makeHTTPRequest(dl);
139     
140     return c;
141 }
142     
143 CatalogRef Catalog::createFromPath(Root* aRoot, const SGPath& aPath)
144 {
145     SGPath xml = aPath;
146     xml.append("catalog.xml");
147     if (!xml.exists()) {
148         return NULL;
149     }
150     
151     SGPropertyNode_ptr props;
152     try {
153         props = new SGPropertyNode;
154         readProperties(xml.str(), props);
155     } catch (sg_exception& e) {
156         return NULL;    
157     }
158     
159     if (props->getStringValue("version") != aRoot->catalogVersion()) {
160         SG_LOG(SG_GENERAL, SG_WARN, "skipping catalog at " << aPath << ", version mismatch:\n\t"
161                << props->getStringValue("version") << " vs required " << aRoot->catalogVersion());
162         return NULL;
163     }
164     
165     CatalogRef c = new Catalog(aRoot);
166     c->m_installRoot = aPath;
167     c->parseProps(props);
168     c->parseTimestamp();
169     
170     return c;
171 }
172
173 PackageList
174 Catalog::packagesMatching(const SGPropertyNode* aFilter) const
175 {
176     PackageList r;
177     BOOST_FOREACH(PackageRef p, m_packages) {
178         if (p->matches(aFilter)) {
179             r.push_back(p);
180         }
181     }
182     return r;
183 }
184
185 PackageList
186 Catalog::packagesNeedingUpdate() const
187 {
188     PackageList r;
189     BOOST_FOREACH(PackageRef p, m_packages) {
190         if (!p->isInstalled()) {
191             continue;
192         }
193         
194         if (p->install()->hasUpdate()) {
195             r.push_back(p);
196         }
197     }
198     return r;
199 }
200
201 PackageList
202 Catalog::installedPackages() const
203 {
204   PackageList r;
205   BOOST_FOREACH(PackageRef p, m_packages) {
206     if (p->isInstalled()) {
207       r.push_back(p);
208     }
209   }
210 }
211   
212 void Catalog::refresh()
213 {
214     Downloader* dl = new Downloader(this, url());
215     m_root->makeHTTPRequest(dl);
216     m_root->catalogRefreshBegin(this);
217 }
218
219 void Catalog::parseProps(const SGPropertyNode* aProps)
220 {
221     // copy everything except package children?
222     m_props = new SGPropertyNode;
223     
224     int nChildren = aProps->nChildren();
225     for (int i = 0; i < nChildren; i++) {
226         const SGPropertyNode* pkgProps = aProps->getChild(i);
227         if (strcmp(pkgProps->getName(), "package") == 0) {
228             PackageRef p = new Package(pkgProps, this);
229             m_packages.push_back(p);   
230         } else {
231             SGPropertyNode* c = m_props->getChild(pkgProps->getName(), pkgProps->getIndex(), true);
232             copyProperties(pkgProps, c);
233         }
234     } // of children iteration
235   
236     if (!m_url.empty()) {
237         if (m_url != m_props->getStringValue("url")) {
238             // this effectively allows packages to migrate to new locations,
239             // although if we're going to rely on that feature we should
240             // maybe formalise it!
241             SG_LOG(SG_GENERAL, SG_WARN, "package downloaded from:" << m_url
242                    << " is now at: " << m_props->getStringValue("url"));
243         }
244     }
245   
246     m_url = m_props->getStringValue("url");
247
248     if (m_installRoot.isNull()) {
249         m_installRoot = m_root->path();
250         m_installRoot.append(id());
251         
252         Dir d(m_installRoot);
253         d.create(0755);
254     }
255 }
256
257 PackageRef Catalog::getPackageById(const std::string& aId) const
258 {
259     BOOST_FOREACH(PackageRef p, m_packages) {
260         if (p->id() == aId) {
261             return p;
262         }
263     }
264     
265     return NULL; // not found
266 }
267
268 std::string Catalog::id() const
269 {
270     return m_props->getStringValue("id");
271 }
272
273 std::string Catalog::url() const
274 {
275     return m_url;
276 }
277
278 std::string Catalog::description() const
279 {
280     return getLocalisedString(m_props, "description");
281 }
282     
283 SGPropertyNode* Catalog::properties() const
284 {
285     return m_props.ptr();
286 }
287
288 void Catalog::parseTimestamp()
289 {
290     SGPath timestampFile = m_installRoot;
291     timestampFile.append(".timestamp");
292     std::ifstream f(timestampFile.c_str(), std::ios::in);
293     f >> m_retrievedTime;
294 }
295
296 void Catalog::writeTimestamp()
297 {
298     SGPath timestampFile = m_installRoot;
299     timestampFile.append(".timestamp");
300     std::ofstream f(timestampFile.c_str(), std::ios::out | std::ios::trunc);
301     f << m_retrievedTime << std::endl;
302 }
303
304 unsigned int Catalog::ageInSeconds() const
305 {
306     time_t now;
307     time(&now);
308     int diff = ::difftime(now, m_retrievedTime);
309     return (diff < 0) ? 0 : diff;
310 }
311
312 bool Catalog::needsRefresh() const
313 {
314     unsigned int maxAge = m_props->getIntValue("max-age-sec", m_root->maxAgeSeconds());
315     return (ageInSeconds() > maxAge);
316 }
317     
318 std::string Catalog::getLocalisedString(const SGPropertyNode* aRoot, const char* aName) const
319 {
320     if (aRoot->hasChild(m_root->getLocale())) {
321         const SGPropertyNode* localeRoot = aRoot->getChild(m_root->getLocale().c_str());
322         if (localeRoot->hasChild(aName)) {
323             return localeRoot->getStringValue(aName);
324         }
325     }
326     
327     return aRoot->getStringValue(aName);
328 }
329
330 void Catalog::refreshComplete(Delegate::FailureCode aReason)
331 {
332     m_root->catalogRefreshComplete(this, aReason);
333 }
334
335
336 } // of namespace pkg
337
338 } // of namespace simgear