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/Package.hxx>
21 #include <boost/foreach.hpp>
22 #include <boost/algorithm/string/case_conv.hpp>
24 #include <simgear/debug/logstream.hxx>
25 #include <simgear/structure/exception.hxx>
27 #include <simgear/package/Catalog.hxx>
28 #include <simgear/package/Install.hxx>
29 #include <simgear/package/Root.hxx>
35 Package::Package(const SGPropertyNode* aProps, CatalogRef aCatalog) :
38 initWithProps(aProps);
41 void Package::initWithProps(const SGPropertyNode* aProps)
43 m_props = const_cast<SGPropertyNode*>(aProps);
45 BOOST_FOREACH(const SGPropertyNode* c, aProps->getChildren("tag")) {
46 std::string t(c->getStringValue());
47 m_tags.insert(boost::to_lower_copy(t));
50 m_id = m_props->getStringValue("id");
53 void Package::updateFromProps(const SGPropertyNode* aProps)
56 initWithProps(aProps);
59 bool Package::matches(const SGPropertyNode* aFilter) const
61 int nChildren = aFilter->nChildren();
62 for (int i = 0; i < nChildren; i++) {
63 const SGPropertyNode* c = aFilter->getChild(i);
64 const std::string& filter_name = c->getNameString();
66 if (strutils::starts_with(filter_name, "rating-")) {
67 int minRating = c->getIntValue();
68 std::string rname = c->getName() + 7;
69 int ourRating = m_props->getChild("rating")->getIntValue(rname, 0);
70 if (ourRating < minRating) {
74 else if (filter_name == "tag") {
75 std::string tag(c->getStringValue());
77 if (m_tags.find(tag) == m_tags.end()) {
81 // substring search of name, description
82 else if (filter_name == "name") {
83 std::string n(c->getStringValue());
85 size_t pos = boost::to_lower_copy(name()).find(n);
86 if (pos == std::string::npos) {
90 else if (filter_name == "description") {
91 std::string n(c->getStringValue());
93 size_t pos = boost::to_lower_copy(description()).find(n);
94 if (pos == std::string::npos) {
98 else if (filter_name == "installed") {
99 if (isInstalled() != c->getBoolValue()) {
104 SG_LOG(SG_GENERAL, SG_WARN, "unknown filter term:" << filter_name);
105 } // of filter props iteration
110 bool Package::isInstalled() const
112 // anything to check for? look for a valid revision file?
113 return pathOnDisk().exists();
116 SGPath Package::pathOnDisk() const
118 SGPath p(m_catalog->installRoot());
119 p.append("Aircraft");
124 InstallRef Package::install()
126 InstallRef ins = existingInstall();
131 // start a new install
132 ins = new Install(this, pathOnDisk());
133 m_catalog->root()->scheduleToUpdate(ins);
135 _install_cb(this, ins);
140 InstallRef Package::existingInstall(const InstallCallback& cb) const
142 InstallRef install = m_catalog->root()->existingInstallForPackage(const_cast<Package*>(this));
146 _install_cb.push_back(cb);
149 cb(const_cast<Package*>(this), install);
155 std::string Package::id() const
160 std::string Package::qualifiedId() const
162 return m_catalog->id() + "." + id();
165 std::string Package::md5() const
167 return m_props->getStringValue("md5");
170 unsigned int Package::revision() const
176 return m_props->getIntValue("revision");
179 std::string Package::name() const
181 return m_props->getStringValue("name");
184 size_t Package::fileSizeBytes() const
186 return m_props->getIntValue("file-size-bytes");
189 std::string Package::description() const
191 return getLocalisedProp("description");
194 string_set Package::tags() const
199 SGPropertyNode* Package::properties() const
201 return m_props.ptr();
204 string_list Package::thumbnailUrls() const
211 BOOST_FOREACH(SGPropertyNode* dl, m_props->getChildren("thumbnail")) {
212 r.push_back(dl->getStringValue());
217 string_list Package::thumbnails() const
224 BOOST_FOREACH(SGPropertyNode* dl, m_props->getChildren("thumbnail-path")) {
225 r.push_back(dl->getStringValue());
230 string_list Package::downloadUrls() const
237 BOOST_FOREACH(SGPropertyNode* dl, m_props->getChildren("url")) {
238 r.push_back(dl->getStringValue());
243 std::string Package::getLocalisedProp(const std::string& aName) const
245 return getLocalisedString(m_props, aName.c_str());
248 std::string Package::getLocalisedString(const SGPropertyNode* aRoot, const char* aName) const
250 std::string locale = m_catalog->root()->getLocale();
251 if (aRoot->hasChild(locale)) {
252 const SGPropertyNode* localeRoot = aRoot->getChild(locale.c_str());
253 if (localeRoot->hasChild(aName)) {
254 return localeRoot->getStringValue(aName);
258 return aRoot->getStringValue(aName);
261 PackageList Package::dependencies() const
265 BOOST_FOREACH(SGPropertyNode* dep, m_props->getChildren("depends")) {
266 std::string depName = dep->getStringValue("package");
267 unsigned int rev = dep->getIntValue("revision", 0);
269 // prefer local hangar package if possible, in case someone does something
270 // silly with naming. Of course flightgear's aircraft search doesn't know
271 // about hangars, so names still need to be unique.
272 PackageRef depPkg = m_catalog->getPackageById(depName);
274 Root* rt = m_catalog->root();
275 depPkg = rt->getPackageById(depName);
277 throw sg_exception("Couldn't satisfy dependency of " + id() + " : " + depName);
281 if (depPkg->revision() < rev) {
282 throw sg_range_exception("Couldn't find suitable revision of " + depName);
285 // forbid recursive dependency graphs, we don't need that level
286 // of complexity for aircraft resources
287 assert(depPkg->dependencies() == PackageList());
289 result.push_back(depPkg);
295 string_list Package::variants() const
298 result.push_back(id());
300 BOOST_FOREACH(SGPropertyNode* var, m_props->getChildren("variant")) {
301 result.push_back(var->getStringValue("id"));
307 std::string Package::nameForVariant(const std::string& vid) const
313 BOOST_FOREACH(SGPropertyNode* var, m_props->getChildren("variant")) {
314 if (vid == var->getStringValue("id")) {
315 return var->getStringValue("name");
320 throw sg_exception("Unknow variant +" + vid + " in package " + id());
323 } // of namespace pkg
325 } // of namespace simgear