]> git.mxchange.org Git - simgear.git/blob - simgear/package/Package.cxx
Package: fix property type (description)
[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         const std::string& filter_name = c->getNameString();
57
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) {
63                 return false;
64             }
65         }
66         else if (filter_name == "tag") {
67             std::string tag(c->getStringValue());
68             boost::to_lower(tag);
69             if (m_tags.find(tag) == m_tags.end()) {
70                 return false;
71             }
72         }
73         // substring search of name, description
74         else if (filter_name == "name") {
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         else if (filter_name == "description") {
83           std::string n(c->getStringValue());
84           boost::to_lower(n);
85           size_t pos = boost::to_lower_copy(description()).find(n);
86           if (pos == std::string::npos) {
87             return false;
88           }
89         }
90         else
91           SG_LOG(SG_GENERAL, SG_WARN, "unknown filter term:" << filter_name);
92     } // of filter props iteration
93     
94     return true;
95 }
96
97 bool Package::isInstalled() const
98 {
99     // anything to check for? look for a valid revision file?
100     return pathOnDisk().exists();
101 }
102
103 SGPath Package::pathOnDisk() const
104 {
105     SGPath p(m_catalog->installRoot());
106     p.append("Aircraft");
107     p.append(id());
108     return p;
109 }
110
111 InstallRef Package::install()
112 {
113     InstallRef ins = existingInstall();
114     if (ins) {
115         return ins;
116     }
117   
118   // start a new install
119     ins = new Install(this, pathOnDisk());
120     m_catalog->root()->scheduleToUpdate(ins);
121     return ins;
122 }
123
124 InstallRef Package::existingInstall() const
125 {
126     return m_catalog->installForPackage(const_cast<Package*>(this));
127 }
128
129 std::string Package::id() const
130 {
131     return m_props->getStringValue("id");
132 }
133
134 std::string Package::qualifiedId() const
135 {
136     return m_catalog->id() + "." + id();
137 }
138
139 std::string Package::md5() const
140 {
141     return m_props->getStringValue("md5");
142 }
143
144 unsigned int Package::revision() const
145 {
146     return m_props->getIntValue("revision");
147 }
148     
149 std::string Package::name() const
150 {
151     return m_props->getStringValue("name");
152 }
153
154 size_t Package::fileSizeBytes() const
155 {
156     return m_props->getIntValue("file-size-bytes");
157 }
158   
159 std::string Package::description() const
160 {
161     return getLocalisedProp("description");
162 }
163     
164 SGPropertyNode* Package::properties() const
165 {
166     return m_props.ptr();
167 }
168
169 string_list Package::thumbnailUrls() const
170 {
171     string_list r;
172     BOOST_FOREACH(SGPropertyNode* dl, m_props->getChildren("thumbnail")) {
173         r.push_back(dl->getStringValue());
174     }
175     return r;
176 }
177
178 string_list Package::downloadUrls() const
179 {
180     string_list r;
181     BOOST_FOREACH(SGPropertyNode* dl, m_props->getChildren("url")) {
182         r.push_back(dl->getStringValue());
183     }
184     return r;
185 }
186
187 std::string Package::getLocalisedProp(const std::string& aName) const
188 {
189     return getLocalisedString(m_props, aName.c_str());
190 }
191
192 std::string Package::getLocalisedString(const SGPropertyNode* aRoot, const char* aName) const
193 {
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);
199         }
200     }
201     
202     return aRoot->getStringValue(aName);
203 }
204
205 PackageList Package::dependencies() const
206 {
207     PackageList result;
208     
209     BOOST_FOREACH(SGPropertyNode* dep, m_props->getChildren("depends")) {
210         std::string depName = dep->getStringValue("package");
211         unsigned int rev = dep->getIntValue("revision", 0);
212         
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);
217         if (!depPkg) {   
218             Root* rt = m_catalog->root();
219             depPkg = rt->getPackageById(depName);
220             if (!depPkg) {
221                 throw sg_exception("Couldn't satisfy dependency of " + id() + " : " + depName);
222             }
223         }
224         
225         if (depPkg->revision() < rev) {
226             throw sg_range_exception("Couldn't find suitable revision of " + depName);
227         }
228     
229     // forbid recursive dependency graphs, we don't need that level
230     // of complexity for aircraft resources
231         assert(depPkg->dependencies() == PackageList());
232         
233         result.push_back(depPkg);
234     }
235     
236     return result;
237 }
238
239 } // of namespace pkg
240
241 } // of namespace simgear