]> git.mxchange.org Git - simgear.git/blob - simgear/package/Catalog.cxx
Update package classes ownership model.
[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 void Catalog::refresh()
202 {
203     Downloader* dl = new Downloader(this, url());
204     m_root->makeHTTPRequest(dl);
205     m_root->catalogRefreshBegin(this);
206 }
207
208 void Catalog::parseProps(const SGPropertyNode* aProps)
209 {
210     // copy everything except package children?
211     m_props = new SGPropertyNode;
212     
213     int nChildren = aProps->nChildren();
214     for (int i = 0; i < nChildren; i++) {
215         const SGPropertyNode* pkgProps = aProps->getChild(i);
216         if (strcmp(pkgProps->getName(), "package") == 0) {
217             PackageRef p = new Package(pkgProps, this);
218             m_packages.push_back(p);   
219         } else {
220             SGPropertyNode* c = m_props->getChild(pkgProps->getName(), pkgProps->getIndex(), true);
221             copyProperties(pkgProps, c);
222         }
223     } // of children iteration
224   
225     if (!m_url.empty()) {
226         if (m_url != m_props->getStringValue("url")) {
227             // this effectively allows packages to migrate to new locations,
228             // although if we're going to rely on that feature we should
229             // maybe formalise it!
230             SG_LOG(SG_GENERAL, SG_WARN, "package downloaded from:" << m_url
231                    << " is now at: " << m_props->getStringValue("url"));
232         }
233     }
234   
235     m_url = m_props->getStringValue("url");
236
237     if (m_installRoot.isNull()) {
238         m_installRoot = m_root->path();
239         m_installRoot.append(id());
240         
241         Dir d(m_installRoot);
242         d.create(0755);
243     }
244 }
245
246 PackageRef Catalog::getPackageById(const std::string& aId) const
247 {
248     BOOST_FOREACH(PackageRef p, m_packages) {
249         if (p->id() == aId) {
250             return p;
251         }
252     }
253     
254     return NULL; // not found
255 }
256
257 std::string Catalog::id() const
258 {
259     return m_props->getStringValue("id");
260 }
261
262 std::string Catalog::url() const
263 {
264     return m_url;
265 }
266
267 std::string Catalog::description() const
268 {
269     return getLocalisedString(m_props, "description");
270 }
271     
272 SGPropertyNode* Catalog::properties() const
273 {
274     return m_props.ptr();
275 }
276
277 void Catalog::parseTimestamp()
278 {
279     SGPath timestampFile = m_installRoot;
280     timestampFile.append(".timestamp");
281     std::ifstream f(timestampFile.c_str(), std::ios::in);
282     f >> m_retrievedTime;
283 }
284
285 void Catalog::writeTimestamp()
286 {
287     SGPath timestampFile = m_installRoot;
288     timestampFile.append(".timestamp");
289     std::ofstream f(timestampFile.c_str(), std::ios::out | std::ios::trunc);
290     f << m_retrievedTime << std::endl;
291 }
292
293 unsigned int Catalog::ageInSeconds() const
294 {
295     time_t now;
296     time(&now);
297     int diff = ::difftime(now, m_retrievedTime);
298     return (diff < 0) ? 0 : diff;
299 }
300
301 bool Catalog::needsRefresh() const
302 {
303     unsigned int maxAge = m_props->getIntValue("max-age-sec", m_root->maxAgeSeconds());
304     return (ageInSeconds() > maxAge);
305 }
306     
307 std::string Catalog::getLocalisedString(const SGPropertyNode* aRoot, const char* aName) const
308 {
309     if (aRoot->hasChild(m_root->getLocale())) {
310         const SGPropertyNode* localeRoot = aRoot->getChild(m_root->getLocale().c_str());
311         if (localeRoot->hasChild(aName)) {
312             return localeRoot->getStringValue(aName);
313         }
314     }
315     
316     return aRoot->getStringValue(aName);
317 }
318
319 void Catalog::refreshComplete(Delegate::FailureCode aReason)
320 {
321     m_root->catalogRefreshComplete(this, aReason);
322 }
323
324
325 } // of namespace pkg
326
327 } // of namespace simgear