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));
51 bool Package::matches(const SGPropertyNode* aFilter) const
53 int nChildren = aFilter->nChildren();
54 for (int i = 0; i < nChildren; i++) {
55 const SGPropertyNode* c = aFilter->getChild(i);
56 if (strutils::starts_with(c->getName(), "rating-")) {
57 int minRating = c->getIntValue();
58 std::string rname = c->getName() + 7;
59 int ourRating = m_props->getChild("rating")->getIntValue(rname, 0);
60 if (ourRating < minRating) {
65 if (strcmp(c->getName(), "tag") == 0) {
66 std::string tag(c->getStringValue());
68 if (m_tags.find(tag) == m_tags.end()) {
73 // substring search of name, description
74 if (strcmp(c->getName(), "name") == 0) {
75 std::string n(c->getStringValue());
77 size_t pos = boost::to_lower_copy(name()).find(n);
78 if (pos == std::string::npos) {
83 if (strcmp(c->getName(), "description") == 0) {
84 std::string n(c->getStringValue());
86 size_t pos = boost::to_lower_copy(description()).find(n);
87 if (pos == std::string::npos) {
92 SG_LOG(SG_GENERAL, SG_WARN, "unknown filter term:" << c->getName());
93 } // of filter props iteration
98 bool Package::isInstalled() const
100 SGPath p(m_catalog->installRoot());
101 p.append("Aircraft");
104 // anything to check for? look for a valid revision file?
108 InstallRef Package::install()
110 SGPath p(m_catalog->installRoot());
111 p.append("Aircraft");
114 return Install::createFromPath(p, m_catalog);
117 InstallRef ins(new Install(this, p));
118 m_catalog->root()->scheduleToUpdate(ins);
122 std::string Package::id() const
124 return m_props->getStringValue("id");
127 std::string Package::md5() const
129 return m_props->getStringValue("md5");
132 unsigned int Package::revision() const
134 return m_props->getIntValue("revision");
137 std::string Package::name() const
139 return m_props->getStringValue("name");
142 std::string Package::description() const
144 return getLocalisedProp("decription");
147 SGPropertyNode* Package::properties() const
149 return m_props.ptr();
152 string_list Package::thumbnailUrls() const
155 BOOST_FOREACH(SGPropertyNode* dl, m_props->getChildren("thumbnail")) {
156 r.push_back(dl->getStringValue());
161 string_list Package::downloadUrls() const
164 BOOST_FOREACH(SGPropertyNode* dl, m_props->getChildren("download")) {
165 r.push_back(dl->getStringValue());
170 std::string Package::getLocalisedProp(const std::string& aName) const
172 return getLocalisedString(m_props, aName.c_str());
175 std::string Package::getLocalisedString(const SGPropertyNode* aRoot, const char* aName) const
177 std::string locale = m_catalog->root()->getLocale();
178 if (aRoot->hasChild(locale)) {
179 const SGPropertyNode* localeRoot = aRoot->getChild(locale.c_str());
180 if (localeRoot->hasChild(aName)) {
181 return localeRoot->getStringValue(aName);
185 return aRoot->getStringValue(aName);
188 PackageList Package::dependencies() const
192 BOOST_FOREACH(SGPropertyNode* dep, m_props->getChildren("depends")) {
193 std::string depName = dep->getStringValue("package");
194 unsigned int rev = dep->getIntValue("revision", 0);
196 // prefer local hangar package if possible, in case someone does something
197 // silly with naming. Of course flightgear's aircraft search doesn't know
198 // about hanagrs, so names still need to be unique.
199 PackageRef depPkg = m_catalog->getPackageById(depName);
201 Root* rt = m_catalog->root();
202 depPkg = rt->getPackageById(depName);
204 throw sg_exception("Couldn't satisfy dependency of " + id() + " : " + depName);
208 if (depPkg->revision() < rev) {
209 throw sg_range_exception("Couldn't find suitable revision of " + depName);
212 // forbid recursive dependency graphs, we don't need that level
213 // of complexity for aircraft resources
214 assert(depPkg->dependencies() == PackageList());
216 result.push_back(depPkg);
222 } // of namespace pkg
224 } // of namespace simgear