Merge pull request #2573 from ulion/slideshow_announce_onplay_speed_zero_for_pausing
[vuplus_xbmc] / xbmc / settings / SettingPath.cpp
1 /*
2  *      Copyright (C) 2013 Team XBMC
3  *      http://www.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 "SettingPath.h"
22 #include "settings/SettingsManager.h"
23 #include "threads/SingleLock.h"
24 #include "utils/log.h"
25 #include "utils/StringUtils.h"
26 #include "utils/XBMCTinyXML.h"
27 #include "utils/XMLUtils.h"
28
29 #define XML_ELM_DEFAULT     "default"
30 #define XML_ELM_CONSTRAINTS "constraints"
31
32 CSettingPath::CSettingPath(const std::string &id, CSettingsManager *settingsManager /* = NULL */)
33   : CSettingString(id, settingsManager),
34     m_writable(true)
35 {
36   m_control.SetType(SettingControlTypeButton);
37   m_control.SetFormat(SettingControlFormatPath);
38   m_control.SetAttributes(SettingControlAttributeNone);
39 }
40   
41 CSettingPath::CSettingPath(const std::string &id, const CSettingPath &setting)
42   : CSettingString(id, setting)
43 {
44   copy(setting);
45 }
46
47 bool CSettingPath::Deserialize(const TiXmlNode *node, bool update /* = false */)
48 {
49   CSingleLock lock(m_critical);
50
51   if (!CSettingString::Deserialize(node, update))
52     return false;
53     
54   if (m_control.GetType() != SettingControlTypeButton ||
55       m_control.GetFormat() != SettingControlFormatPath ||
56       m_control.GetAttributes() != SettingControlAttributeNone)
57   {
58     CLog::Log(LOGERROR, "CSettingPath: invalid <control> of \"%s\"", m_id.c_str());
59     return false;
60   }
61     
62   // get the default value by abusing the FromString
63   // implementation to parse the default value
64   CStdString value;
65   if (XMLUtils::GetString(node, XML_ELM_DEFAULT, value))
66     m_value = m_default = value;
67   else if (!update && !m_allowEmpty)
68   {
69     CLog::Log(LOGERROR, "CSettingPath: error reading the default value of \"%s\"", m_id.c_str());
70     return false;
71   }
72     
73   const TiXmlNode *constraints = node->FirstChild(XML_ELM_CONSTRAINTS);
74   if (constraints != NULL)
75   {
76     // get writable
77     XMLUtils::GetBoolean(constraints, "writable", m_writable);
78
79     // get sources
80     const TiXmlNode *sources = constraints->FirstChild("sources");
81     if (sources != NULL)
82     {
83       m_sources.clear();
84       const TiXmlNode *source = sources->FirstChild("source");
85       while (source != NULL)
86       {
87         std::string strSource = source->FirstChild()->ValueStr();
88         if (!strSource.empty())
89           m_sources.push_back(strSource);
90
91         source = source->NextSibling("source");
92       }
93     }
94   }
95
96   return true;
97 }
98
99 bool CSettingPath::SetValue(const std::string &value)
100 {
101   // for backwards compatibility to Frodo
102   if (StringUtils::EqualsNoCase(value, "select folder") ||
103       StringUtils::EqualsNoCase(value, "select writable folder"))
104     return CSettingString::SetValue("");
105
106   return CSettingString::SetValue(value);
107 }
108
109 void CSettingPath::copy(const CSettingPath &setting)
110 {
111   CSettingString::Copy(setting);
112
113   m_writable = setting.m_writable;
114   m_sources = setting.m_sources;
115 }