]> git.mxchange.org Git - simgear.git/blob - simgear/package/Package.cxx
Package::match case-insensitive string comparison
[simgear.git] / simgear / package / Package.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/Package.hxx>
19
20 #include <cassert>
21 #include <boost/foreach.hpp>
22 #include <boost/algorithm/string/case_conv.hpp>
23
24 #include <simgear/debug/logstream.hxx> 
25 #include <simgear/structure/exception.hxx>
26
27 #include <simgear/package/Catalog.hxx>
28 #include <simgear/package/Install.hxx>
29 #include <simgear/package/Root.hxx>
30
31 namespace simgear {
32     
33 namespace pkg {
34
35 Package::Package(const SGPropertyNode* aProps, CatalogRef aCatalog) :
36     m_catalog(aCatalog)
37 {
38     initWithProps(aProps);
39 }
40
41 void Package::initWithProps(const SGPropertyNode* aProps)
42 {
43     m_props = const_cast<SGPropertyNode*>(aProps);
44 // cache tag values
45     BOOST_FOREACH(const SGPropertyNode* c, aProps->getChildren("tag")) {
46       std::string t(c->getStringValue());
47       m_tags.insert(boost::to_lower_copy(t));
48     }
49 }
50
51 bool Package::matches(const SGPropertyNode* aFilter) const
52 {
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) {
61                 return false;
62             }
63         }
64         
65         if (strcmp(c->getName(), "tag") == 0) {
66             std::string tag(c->getStringValue());
67             boost::to_lower(tag);
68             if (m_tags.find(tag) == m_tags.end()) {
69                 return false;
70             }
71         }
72       
73         // substring search of name, description
74         if (strcmp(c->getName(), "name") == 0) {
75           std::string n(c->getStringValue());
76           boost::to_lower(n);
77           size_t pos = boost::to_lower_copy(name()).find(n);
78           if (pos == std::string::npos) {
79             return false;
80           }
81         }
82       
83         if (strcmp(c->getName(), "description") == 0) {
84           std::string n(c->getStringValue());
85           boost::to_lower(n);
86           size_t pos = boost::to_lower_copy(description()).find(n);
87           if (pos == std::string::npos) {
88             return false;
89           }
90         }
91
92         SG_LOG(SG_GENERAL, SG_WARN, "unknown filter term:" << c->getName());
93     } // of filter props iteration
94     
95     return true;
96 }
97
98 bool Package::isInstalled() const
99 {
100     SGPath p(m_catalog->installRoot());
101     p.append("Aircraft");
102     p.append(id());
103     
104     // anything to check for? look for a valid revision file?
105     return p.exists();
106 }
107
108 InstallRef Package::install()
109 {
110     SGPath p(m_catalog->installRoot());
111     p.append("Aircraft");
112     p.append(id());
113     if (p.exists()) {
114         return Install::createFromPath(p, m_catalog);
115     }
116     
117     InstallRef ins(new Install(this, p));
118     m_catalog->root()->scheduleToUpdate(ins);
119     return ins;
120 }
121
122 std::string Package::id() const
123 {
124     return m_props->getStringValue("id");
125 }
126
127 std::string Package::md5() const
128 {
129     return m_props->getStringValue("md5");
130 }
131
132 unsigned int Package::revision() const
133 {
134     return m_props->getIntValue("revision");
135 }
136     
137 std::string Package::name() const
138 {
139     return m_props->getStringValue("name");
140 }
141
142 std::string Package::description() const
143 {
144     return getLocalisedProp("decription");
145 }
146     
147 SGPropertyNode* Package::properties() const
148 {
149     return m_props.ptr();
150 }
151
152 string_list Package::thumbnailUrls() const
153 {
154     string_list r;
155     BOOST_FOREACH(SGPropertyNode* dl, m_props->getChildren("thumbnail")) {
156         r.push_back(dl->getStringValue());
157     }
158     return r;
159 }
160
161 string_list Package::downloadUrls() const
162 {
163     string_list r;
164     BOOST_FOREACH(SGPropertyNode* dl, m_props->getChildren("download")) {
165         r.push_back(dl->getStringValue());
166     }
167     return r;
168 }
169
170 std::string Package::getLocalisedProp(const std::string& aName) const
171 {
172     return getLocalisedString(m_props, aName.c_str());
173 }
174
175 std::string Package::getLocalisedString(const SGPropertyNode* aRoot, const char* aName) const
176 {
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);
182         }
183     }
184     
185     return aRoot->getStringValue(aName);
186 }
187
188 PackageList Package::dependencies() const
189 {
190     PackageList result;
191     
192     BOOST_FOREACH(SGPropertyNode* dep, m_props->getChildren("depends")) {
193         std::string depName = dep->getStringValue("package");
194         unsigned int rev = dep->getIntValue("revision", 0);
195         
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);
200         if (!depPkg) {   
201             Root* rt = m_catalog->root();
202             depPkg = rt->getPackageById(depName);
203             if (!depPkg) {
204                 throw sg_exception("Couldn't satisfy dependency of " + id() + " : " + depName);
205             }
206         }
207         
208         if (depPkg->revision() < rev) {
209             throw sg_range_exception("Couldn't find suitable revision of " + depName);
210         }
211     
212     // forbid recursive dependency graphs, we don't need that level
213     // of complexity for aircraft resources
214         assert(depPkg->dependencies() == PackageList());
215         
216         result.push_back(depPkg);
217     }
218     
219     return result;
220 }
221
222 } // of namespace pkg
223
224 } // of namespace simgear