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 const std::string& filter_name = c->getNameString();
58 if (strutils::starts_with(filter_name, "rating-")) {
59 int minRating = c->getIntValue();
60 std::string rname = c->getName() + 7;
61 int ourRating = m_props->getChild("rating")->getIntValue(rname, 0);
62 if (ourRating < minRating) {
66 else if (filter_name == "tag") {
67 std::string tag(c->getStringValue());
69 if (m_tags.find(tag) == m_tags.end()) {
73 // substring search of name, description
74 else if (filter_name == "name") {
75 std::string n(c->getStringValue());
77 size_t pos = boost::to_lower_copy(name()).find(n);
78 if (pos == std::string::npos) {
82 else if (filter_name == "description") {
83 std::string n(c->getStringValue());
85 size_t pos = boost::to_lower_copy(description()).find(n);
86 if (pos == std::string::npos) {
91 SG_LOG(SG_GENERAL, SG_WARN, "unknown filter term:" << filter_name);
92 } // of filter props iteration
97 bool Package::isInstalled() const
99 // anything to check for? look for a valid revision file?
100 return pathOnDisk().exists();
103 SGPath Package::pathOnDisk() const
105 SGPath p(m_catalog->installRoot());
106 p.append("Aircraft");
111 InstallRef Package::install()
113 InstallRef ins = existingInstall();
118 // start a new install
119 ins = new Install(this, pathOnDisk());
120 m_catalog->root()->scheduleToUpdate(ins);
124 InstallRef Package::existingInstall() const
126 return m_catalog->installForPackage(const_cast<Package*>(this));
129 std::string Package::id() const
131 return m_props->getStringValue("id");
134 std::string Package::qualifiedId() const
136 return m_catalog->id() + "." + id();
139 std::string Package::md5() const
141 return m_props->getStringValue("md5");
144 unsigned int Package::revision() const
146 return m_props->getIntValue("revision");
149 std::string Package::name() const
151 return m_props->getStringValue("name");
154 size_t Package::fileSizeBytes() const
156 return m_props->getIntValue("file-size-bytes");
159 std::string Package::description() const
161 return getLocalisedProp("description");
164 SGPropertyNode* Package::properties() const
166 return m_props.ptr();
169 string_list Package::thumbnailUrls() const
172 BOOST_FOREACH(SGPropertyNode* dl, m_props->getChildren("thumbnail")) {
173 r.push_back(dl->getStringValue());
178 string_list Package::downloadUrls() const
181 BOOST_FOREACH(SGPropertyNode* dl, m_props->getChildren("url")) {
182 r.push_back(dl->getStringValue());
187 std::string Package::getLocalisedProp(const std::string& aName) const
189 return getLocalisedString(m_props, aName.c_str());
192 std::string Package::getLocalisedString(const SGPropertyNode* aRoot, const char* aName) const
194 std::string locale = m_catalog->root()->getLocale();
195 if (aRoot->hasChild(locale)) {
196 const SGPropertyNode* localeRoot = aRoot->getChild(locale.c_str());
197 if (localeRoot->hasChild(aName)) {
198 return localeRoot->getStringValue(aName);
202 return aRoot->getStringValue(aName);
205 PackageList Package::dependencies() const
209 BOOST_FOREACH(SGPropertyNode* dep, m_props->getChildren("depends")) {
210 std::string depName = dep->getStringValue("package");
211 unsigned int rev = dep->getIntValue("revision", 0);
213 // prefer local hangar package if possible, in case someone does something
214 // silly with naming. Of course flightgear's aircraft search doesn't know
215 // about hanagrs, so names still need to be unique.
216 PackageRef depPkg = m_catalog->getPackageById(depName);
218 Root* rt = m_catalog->root();
219 depPkg = rt->getPackageById(depName);
221 throw sg_exception("Couldn't satisfy dependency of " + id() + " : " + depName);
225 if (depPkg->revision() < rev) {
226 throw sg_range_exception("Couldn't find suitable revision of " + depName);
229 // forbid recursive dependency graphs, we don't need that level
230 // of complexity for aircraft resources
231 assert(depPkg->dependencies() == PackageList());
233 result.push_back(depPkg);
239 } // of namespace pkg
241 } // of namespace simgear