2 * Copyright (C) 2013-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
9 #include "SettingPath.h"
11 #include "settings/lib/SettingsManager.h"
12 #include "utils/FileExtensionProvider.h"
13 #include "utils/StringUtils.h"
14 #include "utils/XBMCTinyXML.h"
15 #include "utils/XMLUtils.h"
16 #include "utils/log.h"
20 #define XML_ELM_DEFAULT "default"
21 #define XML_ELM_CONSTRAINTS "constraints"
23 CSettingPath::CSettingPath(const std::string
&id
, CSettingsManager
*settingsManager
/* = nullptr */)
24 : CSettingString(id
, settingsManager
)
27 CSettingPath::CSettingPath(const std::string
&id
, int label
, const std::string
&value
, CSettingsManager
*settingsManager
/* = nullptr */)
28 : CSettingString(id
, label
, value
, settingsManager
)
31 CSettingPath::CSettingPath(const std::string
&id
, const CSettingPath
&setting
)
32 : CSettingString(id
, setting
)
37 SettingPtr
CSettingPath::Clone(const std::string
&id
) const
39 return std::make_shared
<CSettingPath
>(id
, *this);
42 bool CSettingPath::Deserialize(const TiXmlNode
*node
, bool update
/* = false */)
44 std::unique_lock
<CSharedSection
> lock(m_critical
);
46 if (!CSettingString::Deserialize(node
, update
))
49 if (m_control
!= nullptr &&
50 (m_control
->GetType() != "button" || (m_control
->GetFormat() != "path" && m_control
->GetFormat() != "file" && m_control
->GetFormat() != "image")))
52 CLog::Log(LOGERROR
, "CSettingPath: invalid <control> of \"{}\"", m_id
);
56 auto constraints
= node
->FirstChild(XML_ELM_CONSTRAINTS
);
57 if (constraints
!= nullptr)
60 XMLUtils::GetBoolean(constraints
, "writable", m_writable
);
61 // get hide extensions
62 XMLUtils::GetBoolean(constraints
, "hideextensions", m_hideExtension
);
65 auto sources
= constraints
->FirstChild("sources");
66 if (sources
!= nullptr)
69 auto source
= sources
->FirstChild("source");
70 while (source
!= nullptr)
72 auto child
= source
->FirstChild();
75 const std::string
& strSource
= child
->ValueStr();
76 if (!strSource
.empty())
77 m_sources
.push_back(strSource
);
80 source
= source
->NextSibling("source");
85 auto masking
= constraints
->FirstChild("masking");
86 if (masking
!= nullptr)
87 m_masking
= masking
->FirstChild()->ValueStr();
93 bool CSettingPath::SetValue(const std::string
&value
)
95 // for backwards compatibility to Frodo
96 if (StringUtils::EqualsNoCase(value
, "select folder") ||
97 StringUtils::EqualsNoCase(value
, "select writable folder"))
98 return CSettingString::SetValue("");
100 return CSettingString::SetValue(value
);
103 std::string
CSettingPath::GetMasking(const CFileExtensionProvider
& fileExtensionProvider
) const
105 if (m_masking
.empty())
109 auto audioMask
= fileExtensionProvider
.GetMusicExtensions();
110 auto videoMask
= fileExtensionProvider
.GetVideoExtensions();
111 auto imageMask
= fileExtensionProvider
.GetPictureExtensions();
113 #if defined(TARGET_WINDOWS)
114 execMask
= ".exe|.bat|.cmd|.py";
115 #endif // defined(TARGET_WINDOWS)
117 std::string masking
= m_masking
;
118 if (masking
== "video")
120 if (masking
== "audio")
122 if (masking
== "image")
124 if (masking
== "executable")
127 // convert mask qualifiers
128 StringUtils::Replace(masking
, "$AUDIO", audioMask
);
129 StringUtils::Replace(masking
, "$VIDEO", videoMask
);
130 StringUtils::Replace(masking
, "$IMAGE", imageMask
);
131 StringUtils::Replace(masking
, "$EXECUTABLE", execMask
);
136 void CSettingPath::copy(const CSettingPath
& setting
)
138 CSettingString::Copy(setting
);
140 std::unique_lock
<CSharedSection
> lock(m_critical
);
141 m_writable
= setting
.m_writable
;
142 m_sources
= setting
.m_sources
;
143 m_hideExtension
= setting
.m_hideExtension
;
144 m_masking
= setting
.m_masking
;