3 #include <simgear/package/Package.hxx>
6 #include <boost/foreach.hpp>
8 #include <simgear/debug/logstream.hxx>
9 #include <simgear/structure/exception.hxx>
11 #include <simgear/package/Catalog.hxx>
12 #include <simgear/package/Install.hxx>
13 #include <simgear/package/Root.hxx>
19 Package::Package(const SGPropertyNode* aProps, Catalog* aCatalog) :
22 initWithProps(aProps);
25 void Package::initWithProps(const SGPropertyNode* aProps)
27 m_props = const_cast<SGPropertyNode*>(aProps);
29 BOOST_FOREACH(const SGPropertyNode* c, aProps->getChildren("tag")) {
30 m_tags.insert(c->getStringValue());
34 bool Package::matches(const SGPropertyNode* aFilter) const
36 int nChildren = aFilter->nChildren();
37 for (int i = 0; i < nChildren; i++) {
38 const SGPropertyNode* c = aFilter->getChild(i);
39 if (strutils::starts_with(c->getName(), "rating-")) {
40 int minRating = c->getIntValue();
41 std::string rname = c->getName() + 7;
42 int ourRating = m_props->getChild("rating")->getIntValue(rname, 0);
43 if (ourRating < minRating) {
48 if (strcmp(c->getName(), "tag") == 0) {
49 std::string tag(c->getStringValue());
50 if (m_tags.find(tag) == m_tags.end()) {
55 SG_LOG(SG_GENERAL, SG_WARN, "unknown filter term:" << c->getName());
56 } // of filter props iteration
61 bool Package::isInstalled() const
63 SGPath p(m_catalog->installRoot());
67 // anything to check for? look for a valid revision file?
71 Install* Package::install()
73 SGPath p(m_catalog->installRoot());
77 return Install::createFromPath(p, m_catalog);
80 Install* ins = new Install(this, p);
81 m_catalog->root()->scheduleToUpdate(ins);
85 std::string Package::id() const
87 return m_props->getStringValue("id");
90 std::string Package::md5() const
92 return m_props->getStringValue("md5");
95 unsigned int Package::revision() const
97 return m_props->getIntValue("revision");
100 SGPropertyNode* Package::properties() const
102 return m_props.ptr();
105 string_list Package::thumbnailUrls() const
108 BOOST_FOREACH(SGPropertyNode* dl, m_props->getChildren("thumbnail")) {
109 r.push_back(dl->getStringValue());
114 string_list Package::downloadUrls() const
117 BOOST_FOREACH(SGPropertyNode* dl, m_props->getChildren("download")) {
118 r.push_back(dl->getStringValue());
123 std::string Package::getLocalisedProp(const std::string& aName) const
125 return getLocalisedString(m_props, aName.c_str());
128 std::string Package::getLocalisedString(const SGPropertyNode* aRoot, const char* aName) const
130 std::string locale = m_catalog->root()->getLocale();
131 if (aRoot->hasChild(locale)) {
132 const SGPropertyNode* localeRoot = aRoot->getChild(locale.c_str());
133 if (localeRoot->hasChild(aName)) {
134 return localeRoot->getStringValue(aName);
138 return aRoot->getStringValue(aName);
141 PackageList Package::dependencies() const
145 BOOST_FOREACH(SGPropertyNode* dep, m_props->getChildren("depends")) {
146 std::string depName = dep->getStringValue("package");
147 unsigned int rev = dep->getIntValue("revision", 0);
149 // prefer local hangar package if possible, in case someone does something
150 // silly with naming. Of course flightgear's aircraft search doesn't know
151 // about hanagrs, so names still need to be unique.
152 Package* depPkg = m_catalog->getPackageById(depName);
154 Root* rt = m_catalog->root();
155 depPkg = rt->getPackageById(depName);
157 throw sg_exception("Couldn't satisfy dependency of " + id() + " : " + depName);
161 if (depPkg->revision() < rev) {
162 throw sg_range_exception("Couldn't find suitable revision of " + depName);
165 // forbid recursive dependency graphs, we don't need that level
166 // of complexity for aircraft resources
167 assert(depPkg->dependencies() == PackageList());
169 result.push_back(depPkg);
175 } // of namespace pkg
177 } // of namespace simgear