]> git.mxchange.org Git - simgear.git/blob - simgear/package/Package.cxx
Fix #1783: repeated error message on console
[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     m_id = m_props->getStringValue("id");
51 }
52
53 void Package::updateFromProps(const SGPropertyNode* aProps)
54 {
55     m_tags.clear();
56     initWithProps(aProps);
57 }
58
59 bool Package::matches(const SGPropertyNode* aFilter) const
60 {
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();
65
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) {
71                 return false;
72             }
73         }
74         else if (filter_name == "tag") {
75             std::string tag(c->getStringValue());
76             boost::to_lower(tag);
77             if (m_tags.find(tag) == m_tags.end()) {
78                 return false;
79             }
80         }
81         // substring search of name, description
82         else if (filter_name == "name") {
83           std::string n(c->getStringValue());
84           boost::to_lower(n);
85           size_t pos = boost::to_lower_copy(name()).find(n);
86           if (pos == std::string::npos) {
87             return false;
88           }
89         }
90         else if (filter_name == "description") {
91           std::string n(c->getStringValue());
92           boost::to_lower(n);
93           size_t pos = boost::to_lower_copy(description()).find(n);
94           if (pos == std::string::npos) {
95             return false;
96           }
97         }
98         else if (filter_name == "installed") {
99           if (isInstalled() != c->getBoolValue()) {
100             return false;
101           }
102         }
103         else
104           SG_LOG(SG_GENERAL, SG_WARN, "unknown filter term:" << filter_name);
105     } // of filter props iteration
106     
107     return true;
108 }
109
110 bool Package::isInstalled() const
111 {
112     // anything to check for? look for a valid revision file?
113     return pathOnDisk().exists();
114 }
115
116 SGPath Package::pathOnDisk() const
117 {
118     SGPath p(m_catalog->installRoot());
119     p.append("Aircraft");
120     p.append(id());
121     return p;
122 }
123
124 InstallRef Package::install()
125 {
126     InstallRef ins = existingInstall();
127     if (ins) {
128         return ins;
129     }
130   
131   // start a new install
132     ins = new Install(this, pathOnDisk());
133     m_catalog->root()->scheduleToUpdate(ins);
134
135     _install_cb(this, ins);
136
137     return ins;
138 }
139
140 InstallRef Package::existingInstall(const InstallCallback& cb) const
141 {
142   InstallRef install = m_catalog->installForPackage(const_cast<Package*>(this));
143
144   if( cb )
145   {
146     _install_cb.push_back(cb);
147
148     if( install )
149       cb(const_cast<Package*>(this), install);
150   }
151
152   return install;
153 }
154
155 std::string Package::id() const
156 {
157     return m_id;
158 }
159
160 std::string Package::qualifiedId() const
161 {
162     return m_catalog->id() + "." + id();
163 }
164
165 std::string Package::md5() const
166 {
167     return m_props->getStringValue("md5");
168 }
169
170 unsigned int Package::revision() const
171 {
172     return m_props->getIntValue("revision");
173 }
174     
175 std::string Package::name() const
176 {
177     return m_props->getStringValue("name");
178 }
179
180 size_t Package::fileSizeBytes() const
181 {
182     return m_props->getIntValue("file-size-bytes");
183 }
184   
185 std::string Package::description() const
186 {
187     return getLocalisedProp("description");
188 }
189
190 string_set Package::tags() const
191 {
192     return m_tags;
193 }
194     
195 SGPropertyNode* Package::properties() const
196 {
197     return m_props.ptr();
198 }
199
200 string_list Package::thumbnailUrls() const
201 {
202     string_list r;
203     BOOST_FOREACH(SGPropertyNode* dl, m_props->getChildren("thumbnail")) {
204         r.push_back(dl->getStringValue());
205     }
206     return r;
207 }
208
209 string_list Package::downloadUrls() const
210 {
211     string_list r;
212     BOOST_FOREACH(SGPropertyNode* dl, m_props->getChildren("url")) {
213         r.push_back(dl->getStringValue());
214     }
215     return r;
216 }
217
218 std::string Package::getLocalisedProp(const std::string& aName) const
219 {
220     return getLocalisedString(m_props, aName.c_str());
221 }
222
223 std::string Package::getLocalisedString(const SGPropertyNode* aRoot, const char* aName) const
224 {
225     std::string locale = m_catalog->root()->getLocale();
226     if (aRoot->hasChild(locale)) {
227         const SGPropertyNode* localeRoot = aRoot->getChild(locale.c_str());
228         if (localeRoot->hasChild(aName)) {
229             return localeRoot->getStringValue(aName);
230         }
231     }
232     
233     return aRoot->getStringValue(aName);
234 }
235
236 PackageList Package::dependencies() const
237 {
238     PackageList result;
239     
240     BOOST_FOREACH(SGPropertyNode* dep, m_props->getChildren("depends")) {
241         std::string depName = dep->getStringValue("package");
242         unsigned int rev = dep->getIntValue("revision", 0);
243         
244     // prefer local hangar package if possible, in case someone does something
245     // silly with naming. Of course flightgear's aircraft search doesn't know
246     // about hangars, so names still need to be unique.
247         PackageRef depPkg = m_catalog->getPackageById(depName);
248         if (!depPkg) {   
249             Root* rt = m_catalog->root();
250             depPkg = rt->getPackageById(depName);
251             if (!depPkg) {
252                 throw sg_exception("Couldn't satisfy dependency of " + id() + " : " + depName);
253             }
254         }
255         
256         if (depPkg->revision() < rev) {
257             throw sg_range_exception("Couldn't find suitable revision of " + depName);
258         }
259     
260     // forbid recursive dependency graphs, we don't need that level
261     // of complexity for aircraft resources
262         assert(depPkg->dependencies() == PackageList());
263         
264         result.push_back(depPkg);
265     }
266     
267     return result;
268 }
269
270 string_list Package::variants() const
271 {
272     string_list result;
273     result.push_back(id());
274
275     BOOST_FOREACH(SGPropertyNode* var, m_props->getChildren("variant")) {
276         result.push_back(var->getStringValue("id"));
277     }
278
279     return result;
280 }
281
282 std::string Package::nameForVariant(const std::string& vid) const
283 {
284     if (vid == id()) {
285         return name();
286     }
287
288     BOOST_FOREACH(SGPropertyNode* var, m_props->getChildren("variant")) {
289         if (vid == var->getStringValue("id")) {
290             return var->getStringValue("name");
291         }
292     }
293
294
295     throw sg_exception("Unknow variant +" + vid + " in package " + id());
296 }
297
298 } // of namespace pkg
299
300 } // of namespace simgear