[controls] don't include gotham positions from defaults.xml if <posx> or <posy> are...
[vuplus_xbmc] / xbmc / guilib / GUIIncludes.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include "GUIIncludes.h"
22 #include "addons/Skin.h"
23 #include "GUIInfoManager.h"
24 #include "utils/log.h"
25 #include "utils/XBMCTinyXML.h"
26 #include "utils/StringUtils.h"
27 #include "interfaces/info/SkinVariable.h"
28
29 using namespace std;
30
31 CGUIIncludes::CGUIIncludes()
32 {
33   m_constantAttributes.insert("x");
34   m_constantAttributes.insert("y");
35   m_constantAttributes.insert("width");
36   m_constantAttributes.insert("height");
37   m_constantAttributes.insert("center");
38   m_constantAttributes.insert("max");
39   m_constantAttributes.insert("min");
40   m_constantAttributes.insert("w");
41   m_constantAttributes.insert("h");
42   m_constantAttributes.insert("time");
43   m_constantAttributes.insert("acceleration");
44   m_constantAttributes.insert("delay");
45   m_constantAttributes.insert("start");
46   m_constantAttributes.insert("end");
47   m_constantAttributes.insert("center");
48   m_constantAttributes.insert("border");
49   
50   m_constantNodes.insert("posx");
51   m_constantNodes.insert("posy");
52   m_constantNodes.insert("left");
53   m_constantNodes.insert("right");
54   m_constantNodes.insert("centerx");
55   m_constantNodes.insert("top");
56   m_constantNodes.insert("bottom");
57   m_constantNodes.insert("centery");
58   m_constantNodes.insert("width");
59   m_constantNodes.insert("height");
60   m_constantNodes.insert("offsetx");
61   m_constantNodes.insert("offsety");
62   m_constantNodes.insert("textoffsetx");
63   m_constantNodes.insert("textoffsety");  
64   m_constantNodes.insert("textwidth");
65   m_constantNodes.insert("spinposx");
66   m_constantNodes.insert("spinposy");
67   m_constantNodes.insert("spinwidth");
68   m_constantNodes.insert("spinheight");
69   m_constantNodes.insert("radioposx");
70   m_constantNodes.insert("radioposy");
71   m_constantNodes.insert("radiowidth");
72   m_constantNodes.insert("radioheight");
73   m_constantNodes.insert("markwidth");
74   m_constantNodes.insert("markheight");
75   m_constantNodes.insert("sliderwidth");
76   m_constantNodes.insert("sliderheight");
77   m_constantNodes.insert("itemgap");
78   m_constantNodes.insert("bordersize");
79   m_constantNodes.insert("timeperimage");
80   m_constantNodes.insert("fadetime");
81   m_constantNodes.insert("pauseatend");
82 }
83
84 CGUIIncludes::~CGUIIncludes()
85 {
86 }
87
88 void CGUIIncludes::ClearIncludes()
89 {
90   m_includes.clear();
91   m_defaults.clear();
92   m_constants.clear();
93   m_skinvariables.clear();
94   m_files.clear();
95 }
96
97 bool CGUIIncludes::LoadIncludes(const CStdString &includeFile)
98 {
99   // check to see if we already have this loaded
100   if (HasIncludeFile(includeFile))
101     return true;
102
103   CXBMCTinyXML doc;
104   if (!doc.LoadFile(includeFile))
105   {
106     CLog::Log(LOGINFO, "Error loading includes.xml file (%s): %s (row=%i, col=%i)", includeFile.c_str(), doc.ErrorDesc(), doc.ErrorRow(), doc.ErrorCol());
107     return false;
108   }
109   // success, load the tags
110   if (LoadIncludesFromXML(doc.RootElement()))
111   {
112     m_files.push_back(includeFile);
113     return true;
114   }
115   return false;
116 }
117
118 bool CGUIIncludes::LoadIncludesFromXML(const TiXmlElement *root)
119 {
120   if (!root || strcmpi(root->Value(), "includes"))
121   {
122     CLog::Log(LOGERROR, "Skin includes must start with the <includes> tag");
123     return false;
124   }
125   const TiXmlElement* node = root->FirstChildElement("include");
126   while (node)
127   {
128     if (node->Attribute("name") && node->FirstChild())
129     {
130       CStdString tagName = node->Attribute("name");
131       m_includes.insert(pair<CStdString, TiXmlElement>(tagName, *node));
132     }
133     else if (node->Attribute("file"))
134     { // load this file in as well
135       LoadIncludes(g_SkinInfo->GetSkinPath(node->Attribute("file")));
136     }
137     node = node->NextSiblingElement("include");
138   }
139   // now defaults
140   node = root->FirstChildElement("default");
141   while (node)
142   {
143     if (node->Attribute("type") && node->FirstChild())
144     {
145       CStdString tagName = node->Attribute("type");
146       m_defaults.insert(pair<CStdString, TiXmlElement>(tagName, *node));
147     }
148     node = node->NextSiblingElement("default");
149   }
150   // and finally constants
151   node = root->FirstChildElement("constant");
152   while (node)
153   {
154     if (node->Attribute("name") && node->FirstChild())
155     {
156       CStdString tagName = node->Attribute("name");
157       m_constants.insert(make_pair(tagName, node->FirstChild()->ValueStr()));
158     }
159     node = node->NextSiblingElement("constant");
160   }
161
162   node = root->FirstChildElement("variable");
163   while (node)
164   {
165     if (node->Attribute("name") && node->FirstChild())
166     {
167       CStdString tagName = node->Attribute("name");
168       m_skinvariables.insert(make_pair(tagName, *node));
169     }
170     node = node->NextSiblingElement("variable");
171   }
172
173   return true;
174 }
175
176 bool CGUIIncludes::HasIncludeFile(const CStdString &file) const
177 {
178   for (iFiles it = m_files.begin(); it != m_files.end(); ++it)
179     if (*it == file) return true;
180   return false;
181 }
182
183 void CGUIIncludes::ResolveIncludes(TiXmlElement *node, std::map<int, bool>* xmlIncludeConditions /* = NULL */)
184 {
185   if (!node)
186     return;
187   ResolveIncludesForNode(node, xmlIncludeConditions);
188
189   TiXmlElement *child = node->FirstChildElement();
190   while (child)
191   {
192     ResolveIncludes(child, xmlIncludeConditions);
193     child = child->NextSiblingElement();
194   }
195 }
196
197 void CGUIIncludes::ResolveIncludesForNode(TiXmlElement *node, std::map<int, bool>* xmlIncludeConditions /* = NULL */)
198 {
199   // we have a node, find any <include file="fileName">tagName</include> tags and replace
200   // recursively with their real includes
201   if (!node) return;
202
203   // First add the defaults if this is for a control
204   CStdString type;
205   if (node->ValueStr() == "control")
206   {
207     type = node->Attribute("type");
208     map<CStdString, TiXmlElement>::const_iterator it = m_defaults.find(type);
209     if (it != m_defaults.end())
210     {
211       // we don't insert <left> et. al. if <posx> or <posy> is specified
212       bool hasPosX(node->FirstChild("posx") != NULL);
213       bool hasPosY(node->FirstChild("posy") != NULL);
214
215       const TiXmlElement &element = (*it).second;
216       const TiXmlElement *tag = element.FirstChildElement();
217       while (tag)
218       {
219         std::string value = tag->ValueStr();
220         bool skip(false);
221         if (hasPosX && (value == "left" || value == "right" || value == "centerx"))
222           skip = true;
223         if (hasPosY && (value == "top" || value == "bottom" || value == "centery"))
224           skip = true;
225         // we insert at the end of block
226         if (!skip)
227           node->InsertEndChild(*tag);
228         tag = tag->NextSiblingElement();
229       }
230     }
231   }
232
233   TiXmlElement *include = node->FirstChildElement("include");
234   while (include && include->FirstChild())
235   {
236     // have an include tag - grab it's tag name and replace it with the real tag contents
237     const char *file = include->Attribute("file");
238     if (file)
239     { // we need to load this include from the alternative file
240       LoadIncludes(g_SkinInfo->GetSkinPath(file));
241     }
242     const char *condition = include->Attribute("condition");
243     if (condition)
244     { // check this condition
245       int conditionID = g_infoManager.Register(condition);
246       bool value = g_infoManager.GetBoolValue(conditionID);
247
248       if (xmlIncludeConditions)
249         (*xmlIncludeConditions)[conditionID] = value;
250
251       if (!value)
252       {
253         include = include->NextSiblingElement("include");
254         continue;
255       }
256     }
257     CStdString tagName = include->FirstChild()->Value();
258     map<CStdString, TiXmlElement>::const_iterator it = m_includes.find(tagName);
259     if (it != m_includes.end())
260     { // found the tag(s) to include - let's replace it
261       const TiXmlElement &element = (*it).second;
262       const TiXmlElement *tag = element.FirstChildElement();
263       while (tag)
264       {
265         // we insert before the <include> element to keep the correct
266         // order (we render in the order given in the xml file)
267         node->InsertBeforeChild(include, *tag);
268         tag = tag->NextSiblingElement();
269       }
270       // remove the <include>tagName</include> element
271       node->RemoveChild(include);
272       include = node->FirstChildElement("include");
273     }
274     else
275     { // invalid include
276       CLog::Log(LOGWARNING, "Skin has invalid include: %s", tagName.c_str());
277       include = include->NextSiblingElement("include");
278     }
279   }
280
281   // run through this element's attributes, resolving any constants
282   TiXmlAttribute *attribute = node->FirstAttribute();
283   while (attribute)
284   { // check the attribute against our set
285     if (m_constantAttributes.count(attribute->Name()))
286       attribute->SetValue(ResolveConstant(attribute->ValueStr()));
287     attribute = attribute->Next();
288   }
289   // also do the value
290   if (node->FirstChild() && node->FirstChild()->Type() == TiXmlNode::TINYXML_TEXT && m_constantNodes.count(node->ValueStr()))
291     node->FirstChild()->SetValue(ResolveConstant(node->FirstChild()->ValueStr()));
292 }
293
294 CStdString CGUIIncludes::ResolveConstant(const CStdString &constant) const
295 {
296   CStdStringArray values;
297   StringUtils::SplitString(constant, ",", values);
298   for (unsigned int i = 0; i < values.size(); ++i)
299   {
300     map<CStdString, CStdString>::const_iterator it = m_constants.find(values[i]);
301     if (it != m_constants.end())
302       values[i] = it->second;
303   }
304   CStdString value;
305   StringUtils::JoinString(values, ",", value);
306   return value;
307 }
308
309 const INFO::CSkinVariableString* CGUIIncludes::CreateSkinVariable(const CStdString& name, int context)
310 {
311   map<CStdString, TiXmlElement>::const_iterator it = m_skinvariables.find(name);
312   if (it != m_skinvariables.end())
313     return INFO::CSkinVariable::CreateFromXML(it->second, context);
314   return NULL;
315 }