Generalise per-variant localised string lookup.

Will be used for per-variant long-descriptions.
This commit is contained in:
James Turner 2016-11-25 22:44:24 +00:00
parent 14cdae5102
commit 64531c85e3
2 changed files with 22 additions and 17 deletions

View File

@ -206,7 +206,7 @@ size_t Package::fileSizeBytes() const
std::string Package::description() const std::string Package::description() const
{ {
return getLocalisedProp("description"); return getLocalisedProp("description", 0);
} }
string_set Package::tags() const string_set Package::tags() const
@ -258,9 +258,9 @@ string_list Package::downloadUrls() const
return r; return r;
} }
std::string Package::getLocalisedProp(const std::string& aName) const std::string Package::getLocalisedProp(const std::string& aName, const unsigned int vIndex) const
{ {
return getLocalisedString(m_props, aName.c_str()); return getLocalisedString(propsForVariant(vIndex, aName.c_str()), aName.c_str());
} }
std::string Package::getLocalisedString(const SGPropertyNode* aRoot, const char* aName) const std::string Package::getLocalisedString(const SGPropertyNode* aRoot, const char* aName) const
@ -358,28 +358,31 @@ unsigned int Package::indexOfVariant(const std::string& vid) const
std::string Package::nameForVariant(const unsigned int vIndex) const std::string Package::nameForVariant(const unsigned int vIndex) const
{ {
if (vIndex == 0) return propsForVariant(vIndex, "name")->getStringValue("name");
return name(); }
SGPropertyNode_ptr Package::propsForVariant(const unsigned int vIndex, const char* propName) const
{
if (vIndex == 0) {
return m_props;
}
// offset by minus one to allow for index 0 being the primary // offset by minus one to allow for index 0 being the primary
SGPropertyNode_ptr var = m_props->getChild("variant", vIndex - 1); SGPropertyNode_ptr var = m_props->getChild("variant", vIndex - 1);
if (var) if (var) {
return var->getStringValue("name"); if (!propName || var->hasChild(propName)) {
return var;
}
return m_props;
}
throw sg_exception("Unknow variant in package " + id()); throw sg_exception("Unknow variant in package " + id());
} }
Package::ThumbnailVec Package::thumbnailsForVariant(unsigned int vIndex) const Package::ThumbnailVec Package::thumbnailsForVariant(unsigned int vIndex) const
{ {
if (vIndex == 0) { SGPropertyNode_ptr var = propsForVariant(vIndex);
return thumbnailsFromProps(m_props);
}
SGPropertyNode_ptr var = m_props->getChild("variant", vIndex - 1);
if (!var) {
throw sg_exception("Unknow variant in package " + id());
}
return thumbnailsFromProps(var); return thumbnailsFromProps(var);
} }

View File

@ -114,7 +114,7 @@ public:
*/ */
std::string md5() const; std::string md5() const;
std::string getLocalisedProp(const std::string& aName) const; std::string getLocalisedProp(const std::string& aName, const unsigned int vIndex = 0) const;
unsigned int revision() const; unsigned int revision() const;
@ -196,6 +196,8 @@ private:
ThumbnailVec thumbnailsFromProps(const SGPropertyNode_ptr& ptr) const; ThumbnailVec thumbnailsFromProps(const SGPropertyNode_ptr& ptr) const;
SGPropertyNode_ptr propsForVariant(const unsigned int vIndex, const char* propName = nullptr) const;
SGPropertyNode_ptr m_props; SGPropertyNode_ptr m_props;
std::string m_id; std::string m_id;
string_set m_tags; string_set m_tags;