FIX: [droid] set "remote as keyboard" default to true
[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("centerleft");
54   m_constantNodes.insert("right");
55   m_constantNodes.insert("centerright");
56   m_constantNodes.insert("top");
57   m_constantNodes.insert("centertop");
58   m_constantNodes.insert("bottom");
59   m_constantNodes.insert("centerbottom");
60   m_constantNodes.insert("width");
61   m_constantNodes.insert("height");
62   m_constantNodes.insert("offsetx");
63   m_constantNodes.insert("offsety");
64   m_constantNodes.insert("textoffsetx");
65   m_constantNodes.insert("textoffsety");  
66   m_constantNodes.insert("textwidth");
67   m_constantNodes.insert("spinposx");
68   m_constantNodes.insert("spinposy");
69   m_constantNodes.insert("spinwidth");
70   m_constantNodes.insert("spinheight");
71   m_constantNodes.insert("radioposx");
72   m_constantNodes.insert("radioposy");
73   m_constantNodes.insert("radiowidth");
74   m_constantNodes.insert("radioheight");
75   m_constantNodes.insert("markwidth");
76   m_constantNodes.insert("markheight");
77   m_constantNodes.insert("sliderwidth");
78   m_constantNodes.insert("sliderheight");
79   m_constantNodes.insert("itemgap");
80   m_constantNodes.insert("bordersize");
81   m_constantNodes.insert("timeperimage");
82   m_constantNodes.insert("fadetime");
83   m_constantNodes.insert("pauseatend");
84 }
85
86 CGUIIncludes::~CGUIIncludes()
87 {
88 }
89
90 void CGUIIncludes::ClearIncludes()
91 {
92   m_includes.clear();
93   m_defaults.clear();
94   m_constants.clear();
95   m_skinvariables.clear();
96   m_files.clear();
97 }
98
99 bool CGUIIncludes::LoadIncludes(const CStdString &includeFile)
100 {
101   // check to see if we already have this loaded
102   if (HasIncludeFile(includeFile))
103     return true;
104
105   CXBMCTinyXML doc;
106   if (!doc.LoadFile(includeFile))
107   {
108     CLog::Log(LOGINFO, "Error loading includes.xml file (%s): %s (row=%i, col=%i)", includeFile.c_str(), doc.ErrorDesc(), doc.ErrorRow(), doc.ErrorCol());
109     return false;
110   }
111   // success, load the tags
112   if (LoadIncludesFromXML(doc.RootElement()))
113   {
114     m_files.push_back(includeFile);
115     return true;
116   }
117   return false;
118 }
119
120 bool CGUIIncludes::LoadIncludesFromXML(const TiXmlElement *root)
121 {
122   if (!root || strcmpi(root->Value(), "includes"))
123   {
124     CLog::Log(LOGERROR, "Skin includes must start with the <includes> tag");
125     return false;
126   }
127   const TiXmlElement* node = root->FirstChildElement("include");
128   while (node)
129   {
130     if (node->Attribute("name") && node->FirstChild())
131     {
132       CStdString tagName = node->Attribute("name");
133       m_includes.insert(pair<CStdString, TiXmlElement>(tagName, *node));
134     }
135     else if (node->Attribute("file"))
136     { // load this file in as well
137       LoadIncludes(g_SkinInfo->GetSkinPath(node->Attribute("file")));
138     }
139     node = node->NextSiblingElement("include");
140   }
141   // now defaults
142   node = root->FirstChildElement("default");
143   while (node)
144   {
145     if (node->Attribute("type") && node->FirstChild())
146     {
147       CStdString tagName = node->Attribute("type");
148       m_defaults.insert(pair<CStdString, TiXmlElement>(tagName, *node));
149     }
150     node = node->NextSiblingElement("default");
151   }
152   // and finally constants
153   node = root->FirstChildElement("constant");
154   while (node)
155   {
156     if (node->Attribute("name") && node->FirstChild())
157     {
158       CStdString tagName = node->Attribute("name");
159       m_constants.insert(make_pair(tagName, node->FirstChild()->ValueStr()));
160     }
161     node = node->NextSiblingElement("constant");
162   }
163
164   node = root->FirstChildElement("variable");
165   while (node)
166   {
167     if (node->Attribute("name") && node->FirstChild())
168     {
169       CStdString tagName = node->Attribute("name");
170       m_skinvariables.insert(make_pair(tagName, *node));
171     }
172     node = node->NextSiblingElement("variable");
173   }
174
175   return true;
176 }
177
178 bool CGUIIncludes::HasIncludeFile(const CStdString &file) const
179 {
180   for (iFiles it = m_files.begin(); it != m_files.end(); ++it)
181     if (*it == file) return true;
182   return false;
183 }
184
185 void CGUIIncludes::ResolveIncludes(TiXmlElement *node, std::map<INFO::InfoPtr, bool>* xmlIncludeConditions /* = NULL */)
186 {
187   if (!node)
188     return;
189   ResolveIncludesForNode(node, xmlIncludeConditions);
190
191   TiXmlElement *child = node->FirstChildElement();
192   while (child)
193   {
194     ResolveIncludes(child, xmlIncludeConditions);
195     child = child->NextSiblingElement();
196   }
197 }
198
199 void CGUIIncludes::ResolveIncludesForNode(TiXmlElement *node, std::map<INFO::InfoPtr, bool>* xmlIncludeConditions /* = NULL */)
200 {
201   // we have a node, find any <include file="fileName">tagName</include> tags and replace
202   // recursively with their real includes
203   if (!node) return;
204
205   // First add the defaults if this is for a control
206   CStdString type;
207   if (node->ValueStr() == "control")
208   {
209     type = node->Attribute("type");
210     map<CStdString, TiXmlElement>::const_iterator it = m_defaults.find(type);
211     if (it != m_defaults.end())
212     {
213       // we don't insert <left> et. al. if <posx> or <posy> is specified
214       bool hasPosX(node->FirstChild("posx") != NULL);
215       bool hasPosY(node->FirstChild("posy") != NULL);
216
217       const TiXmlElement &element = (*it).second;
218       const TiXmlElement *tag = element.FirstChildElement();
219       while (tag)
220       {
221         std::string value = tag->ValueStr();
222         bool skip(false);
223         if (hasPosX && (value == "left" || value == "right" || value == "centerleft" || value == "centerright"))
224           skip = true;
225         if (hasPosY && (value == "top" || value == "bottom" || value == "centertop" || value == "centerbottom"))
226           skip = true;
227         // we insert at the end of block
228         if (!skip)
229           node->InsertEndChild(*tag);
230         tag = tag->NextSiblingElement();
231       }
232     }
233   }
234
235   TiXmlElement *include = node->FirstChildElement("include");
236   while (include && include->FirstChild())
237   {
238     // have an include tag - grab it's tag name and replace it with the real tag contents
239     const char *file = include->Attribute("file");
240     if (file)
241     { // we need to load this include from the alternative file
242       LoadIncludes(g_SkinInfo->GetSkinPath(file));
243     }
244     const char *condition = include->Attribute("condition");
245     if (condition)
246     { // check this condition
247       INFO::InfoPtr conditionID = g_infoManager.Register(condition);
248       bool value = conditionID->Get();
249
250       if (xmlIncludeConditions)
251         (*xmlIncludeConditions)[conditionID] = value;
252
253       if (!value)
254       {
255         include = include->NextSiblingElement("include");
256         continue;
257       }
258     }
259     CStdString tagName = include->FirstChild()->Value();
260     map<CStdString, TiXmlElement>::const_iterator it = m_includes.find(tagName);
261     if (it != m_includes.end())
262     { // found the tag(s) to include - let's replace it
263       const TiXmlElement &element = (*it).second;
264       const TiXmlElement *tag = element.FirstChildElement();
265       while (tag)
266       {
267         // we insert before the <include> element to keep the correct
268         // order (we render in the order given in the xml file)
269         node->InsertBeforeChild(include, *tag);
270         tag = tag->NextSiblingElement();
271       }
272       // remove the <include>tagName</include> element
273       node->RemoveChild(include);
274       include = node->FirstChildElement("include");
275     }
276     else
277     { // invalid include
278       CLog::Log(LOGWARNING, "Skin has invalid include: %s", tagName.c_str());
279       include = include->NextSiblingElement("include");
280     }
281   }
282
283   // run through this element's attributes, resolving any constants
284   TiXmlAttribute *attribute = node->FirstAttribute();
285   while (attribute)
286   { // check the attribute against our set
287     if (m_constantAttributes.count(attribute->Name()))
288       attribute->SetValue(ResolveConstant(attribute->ValueStr()));
289     attribute = attribute->Next();
290   }
291   // also do the value
292   if (node->FirstChild() && node->FirstChild()->Type() == TiXmlNode::TINYXML_TEXT && m_constantNodes.count(node->ValueStr()))
293     node->FirstChild()->SetValue(ResolveConstant(node->FirstChild()->ValueStr()));
294 }
295
296 CStdString CGUIIncludes::ResolveConstant(const CStdString &constant) const
297 {
298   CStdStringArray values;
299   StringUtils::SplitString(constant, ",", values);
300   for (unsigned int i = 0; i < values.size(); ++i)
301   {
302     map<CStdString, CStdString>::const_iterator it = m_constants.find(values[i]);
303     if (it != m_constants.end())
304       values[i] = it->second;
305   }
306   CStdString value;
307   StringUtils::JoinString(values, ",", value);
308   return value;
309 }
310
311 const INFO::CSkinVariableString* CGUIIncludes::CreateSkinVariable(const CStdString& name, int context)
312 {
313   map<CStdString, TiXmlElement>::const_iterator it = m_skinvariables.find(name);
314   if (it != m_skinvariables.end())
315     return INFO::CSkinVariable::CreateFromXML(it->second, context);
316   return NULL;
317 }