[cstdstring] removal of Trim/TrimLeft/TrimRight
[vuplus_xbmc] / xbmc / guilib / GUIColorManager.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 "GUIColorManager.h"
22 #include "filesystem/SpecialProtocol.h"
23 #include "addons/Skin.h"
24 #include "utils/log.h"
25 #include "utils/URIUtils.h"
26 #include "utils/XBMCTinyXML.h"
27 #include "utils/StringUtils.h"
28
29 CGUIColorManager g_colorManager;
30
31 CGUIColorManager::CGUIColorManager(void)
32 {
33 }
34
35 CGUIColorManager::~CGUIColorManager(void)
36 {
37   Clear();
38 }
39
40 void CGUIColorManager::Clear()
41 {
42   m_colors.clear();
43 }
44
45 // load the color file in
46 void CGUIColorManager::Load(const CStdString &colorFile)
47 {
48   Clear();
49
50   // load the global color map if it exists
51   CXBMCTinyXML xmlDoc;
52   if (xmlDoc.LoadFile(CSpecialProtocol::TranslatePathConvertCase("special://xbmc/system/colors.xml")))
53     LoadXML(xmlDoc);
54
55   // first load the default color map if it exists
56   CStdString basePath = URIUtils::AddFileToFolder(g_SkinInfo->Path(), "colors");
57   CStdString path = URIUtils::AddFileToFolder(basePath, "defaults.xml");
58
59   if (xmlDoc.LoadFile(CSpecialProtocol::TranslatePathConvertCase(path)))
60     LoadXML(xmlDoc);
61
62   // now the color map requested
63   if (colorFile.CompareNoCase("SKINDEFAULT") == 0)
64     return; // nothing to do
65
66   path = URIUtils::AddFileToFolder(basePath, colorFile);
67   if (!URIUtils::HasExtension(path))
68     path += ".xml";
69   CLog::Log(LOGINFO, "Loading colors from %s", path.c_str());
70
71   if (xmlDoc.LoadFile(path))
72     LoadXML(xmlDoc);
73 }
74
75 bool CGUIColorManager::LoadXML(CXBMCTinyXML &xmlDoc)
76 {
77   TiXmlElement* pRootElement = xmlDoc.RootElement();
78
79   CStdString strValue = pRootElement->Value();
80   if (strValue != CStdString("colors"))
81   {
82     CLog::Log(LOGERROR, "color file doesnt start with <colors>");
83     return false;
84   }
85
86   const TiXmlElement *color = pRootElement->FirstChildElement("color");
87
88   while (color)
89   {
90     if (color->FirstChild() && color->Attribute("name"))
91     {
92       color_t value = 0xffffffff;
93       sscanf(color->FirstChild()->Value(), "%x", (unsigned int*) &value);
94       CStdString name = color->Attribute("name");
95       iColor it = m_colors.find(name);
96       if (it != m_colors.end())
97         (*it).second = value;
98       else
99         m_colors.insert(make_pair(name, value));
100     }
101     color = color->NextSiblingElement("color");
102   }
103   return true;
104 }
105
106 // lookup a color and return it's hex value
107 color_t CGUIColorManager::GetColor(const CStdString &color) const
108 {
109   // look in our color map
110   CStdString trimmed(color);
111   StringUtils::TrimLeft(trimmed, "= ");
112   icColor it = m_colors.find(trimmed);
113   if (it != m_colors.end())
114     return (*it).second;
115
116   // try converting hex directly
117   color_t value = 0;
118   sscanf(trimmed.c_str(), "%x", &value);
119   return value;
120 }
121