]> git.mxchange.org Git - simgear.git/blob - simgear/package/Package.cxx
Package::existingInstall helper.
[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     // anything to check for? look for a valid revision file?
101     return pathOnDisk().exists();
102 }
103
104 SGPath Package::pathOnDisk() const
105 {
106     SGPath p(m_catalog->installRoot());
107     p.append("Aircraft");
108     p.append(id());
109     return p;
110 }
111
112 InstallRef Package::install()
113 {
114     SGPath p(pathOnDisk());
115     if (p.exists()) {
116         return Install::createFromPath(p, m_catalog);
117     }
118     
119     InstallRef ins(new Install(this, p));
120     m_catalog->root()->scheduleToUpdate(ins);
121     return ins;
122 }
123
124 InstallRef Package::existingInstall() const
125 {
126     SGPath p(pathOnDisk());
127     if (p.exists()) {
128         return Install::createFromPath(p, m_catalog);
129     }
130
131     return NULL;
132 }
133
134 std::string Package::id() const
135 {
136     return m_props->getStringValue("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 std::string Package::description() const
155 {
156     return getLocalisedProp("decription");
157 }
158     
159 SGPropertyNode* Package::properties() const
160 {
161     return m_props.ptr();
162 }
163
164 string_list Package::thumbnailUrls() const
165 {
166     string_list r;
167     BOOST_FOREACH(SGPropertyNode* dl, m_props->getChildren("thumbnail")) {
168         r.push_back(dl->getStringValue());
169     }
170     return r;
171 }
172
173 string_list Package::downloadUrls() const
174 {
175     string_list r;
176     BOOST_FOREACH(SGPropertyNode* dl, m_props->getChildren("url")) {
177         r.push_back(dl->getStringValue());
178     }
179     return r;
180 }
181
182 std::string Package::getLocalisedProp(const std::string& aName) const
183 {
184     return getLocalisedString(m_props, aName.c_str());
185 }
186
187 std::string Package::getLocalisedString(const SGPropertyNode* aRoot, const char* aName) const
188 {
189     std::string locale = m_catalog->root()->getLocale();
190     if (aRoot->hasChild(locale)) {
191         const SGPropertyNode* localeRoot = aRoot->getChild(locale.c_str());
192         if (localeRoot->hasChild(aName)) {
193             return localeRoot->getStringValue(aName);
194         }
195     }
196     
197     return aRoot->getStringValue(aName);
198 }
199
200 PackageList Package::dependencies() const
201 {
202     PackageList result;
203     
204     BOOST_FOREACH(SGPropertyNode* dep, m_props->getChildren("depends")) {
205         std::string depName = dep->getStringValue("package");
206         unsigned int rev = dep->getIntValue("revision", 0);
207         
208     // prefer local hangar package if possible, in case someone does something
209     // silly with naming. Of course flightgear's aircraft search doesn't know
210     // about hanagrs, so names still need to be unique.
211         PackageRef depPkg = m_catalog->getPackageById(depName);
212         if (!depPkg) {   
213             Root* rt = m_catalog->root();
214             depPkg = rt->getPackageById(depName);
215             if (!depPkg) {
216                 throw sg_exception("Couldn't satisfy dependency of " + id() + " : " + depName);
217             }
218         }
219         
220         if (depPkg->revision() < rev) {
221             throw sg_range_exception("Couldn't find suitable revision of " + depName);
222         }
223     
224     // forbid recursive dependency graphs, we don't need that level
225     // of complexity for aircraft resources
226         assert(depPkg->dependencies() == PackageList());
227         
228         result.push_back(depPkg);
229     }
230     
231     return result;
232 }
233
234 } // of namespace pkg
235
236 } // of namespace simgear