Debian Codegraph @

xbmc/pvr/channels/PVRChannelsPath.cpp

raw
1 /*
2 * Copyright (C) 2012-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9 #include "PVRChannelsPath.h"
10
11 #include "URL.h"
12 #include "utils/StringUtils.h"
13 #include "utils/URIUtils.h"
14 #include "utils/log.h"
15
16 #include <string>
17 #include <vector>
18
19 using namespace PVR;
20
21 const std::string CPVRChannelsPath::PATH_TV_CHANNELS = "pvr://channels/tv/";
22 const std::string CPVRChannelsPath::PATH_RADIO_CHANNELS = "pvr://channels/radio/";
23
24
25 CPVRChannelsPath::CPVRChannelsPath(const std::string& strPath)
26 {
27 std::string strVarPath = TrimSlashes(strPath);
28 const std::vector<std::string> segments = URIUtils::SplitPath(strVarPath);
29
30 for (const std::string& segment : segments)
31 {
32 switch (m_kind)
33 {
34 case Kind::INVALID:
35 if (segment == "pvr://")
36 m_kind = Kind::PROTO; // pvr:// followed by something => go on
37 else if (segment == "pvr:" && segments.size() == 1) // just pvr:// => invalid
38 strVarPath = "pvr:/";
39 break;
40
41 case Kind::PROTO:
42 if (segment == "channels")
43 m_kind = Kind::EMPTY; // pvr://channels
44 else
45 m_kind = Kind::INVALID;
46 break;
47
48 case Kind::EMPTY:
49 if (segment == "tv" || segment == "radio")
50 {
51 m_kind = Kind::ROOT; // pvr://channels/(tv|radio)
52 m_bRadio = (segment == "radio");
53 }
54 else
55 {
56 CLog::LogF(LOGERROR, "Invalid channels path '{}' - channel root segment syntax error.",
57 strPath);
58 m_kind = Kind::INVALID;
59 }
60 break;
61
62 case Kind::ROOT:
63 {
64 // special case denoting local all channels group
65 if (segment == "*")
66 {
67 m_kind = Kind::GROUP; // pvr://channels/(tv|radio)/<all-channels-wildcard>@-1
68 m_groupName = segment;
69 m_groupClientID = -1; // local
70 break;
71 }
72
73 std::vector<std::string> tokens = StringUtils::Split(segment, "@");
74 if (tokens.size() == 2 && !tokens[0].empty() && !tokens[1].empty())
75 {
76 m_groupName = CURL::Decode(tokens[0]);
77
78 std::string groupClientID = tokens[1];
79 if (groupClientID.find_first_not_of("-0123456789") == std::string::npos)
80 {
81 m_groupClientID = std::atoi(groupClientID.c_str());
82 if (m_groupClientID >= -1)
83 {
84 m_kind = Kind::GROUP; // pvr://channels/(tv|radio)/<groupname>@<clientid>
85 break;
86 }
87 }
88 }
89
90 CLog::LogF(LOGERROR, "Invalid channels path '{}' - channel group segment syntax error.",
91 strPath);
92 m_kind = Kind::INVALID;
93 break;
94 }
95
96 case Kind::GROUP:
97 {
98 std::vector<std::string> tokens = StringUtils::Split(segment, "_");
99 if (tokens.size() == 2)
100 {
101 std::vector<std::string> instance = StringUtils::Split(tokens[0], "@");
102 if (instance.size() == 2)
103 {
104 m_instanceID = std::atoi(instance[0].c_str());
105 m_addonID = instance[1];
106 }
107 else
108 {
109 m_instanceID = ADDON::ADDON_SINGLETON_INSTANCE_ID;
110 m_addonID = tokens[0];
111 }
112
113 tokens = StringUtils::Split(tokens[1], ".");
114 if (tokens.size() == 2 && tokens[1] == "pvr")
115 {
116 std::string channelUID = tokens[0];
117 if (!channelUID.empty() && channelUID.find_first_not_of("0123456789") == std::string::npos)
118 m_iChannelUID = std::atoi(channelUID.c_str());
119 }
120 }
121
122 if (!m_addonID.empty() && m_iChannelUID >= 0)
123 {
124 m_kind = Kind::
125 CHANNEL; // pvr://channels/(tv|radio)/<groupname>@<clientid>/<instanceid>@<addonid>_<channeluid>.pvr
126 }
127 else
128 {
129 CLog::LogF(LOGERROR, "Invalid channels path '{}' - channel segment syntax error.",
130 strPath);
131 m_kind = Kind::INVALID;
132 }
133 break;
134 }
135
136 case Kind::CHANNEL:
137 CLog::LogF(LOGERROR, "Invalid channels path '{}' - too many path segments.", strPath);
138 m_kind = Kind::INVALID; // too many segments
139 break;
140 }
141
142 if (m_kind == Kind::INVALID)
143 break;
144 }
145
146 // append slash to all folders
147 if (m_kind < Kind::CHANNEL)
148 strVarPath.append("/");
149
150 m_path = strVarPath;
151 }
152
153 CPVRChannelsPath::CPVRChannelsPath(bool bRadio,
154 bool bHidden,
155 const std::string& strGroupName,
156 int iGroupClientID)
157 : m_bRadio(bRadio), m_groupName(bHidden ? ".hidden" : strGroupName)
158 {
159 if (m_groupName.empty())
160 {
161 m_kind = Kind::EMPTY;
162 m_path = StringUtils::Format("pvr://channels/{}", bRadio ? "radio" : "tv");
163 }
164 else
165 {
166 m_kind = Kind::GROUP;
167 m_groupClientID = iGroupClientID;
168 m_path = StringUtils::Format("pvr://channels/{}/{}@{}/", bRadio ? "radio" : "tv",
169 CURL::Encode(m_groupName), m_groupClientID);
170 }
171 }
172
173 CPVRChannelsPath::CPVRChannelsPath(bool bRadio, const std::string& strGroupName, int iGroupClientID)
174 : m_bRadio(bRadio), m_groupName(strGroupName)
175 {
176 if (m_groupName.empty())
177 {
178 m_kind = Kind::EMPTY;
179 m_path = StringUtils::Format("pvr://channels/{}", bRadio ? "radio" : "tv");
180 }
181 else
182 {
183 m_kind = Kind::GROUP;
184 m_groupClientID = iGroupClientID;
185 m_path = StringUtils::Format("pvr://channels/{}/{}@{}/", bRadio ? "radio" : "tv",
186 CURL::Encode(m_groupName), m_groupClientID);
187 }
188 }
189
190 CPVRChannelsPath::CPVRChannelsPath(bool bRadio,
191 const std::string& strGroupName,
192 int iGroupClientID,
193 const std::string& strAddonID,
194 ADDON::AddonInstanceId instanceID,
195 int iChannelUID)
196 : m_bRadio(bRadio)
197 {
198 if (!strGroupName.empty() && iGroupClientID >= -1 && !strAddonID.empty() && iChannelUID >= 0)
199 {
200 m_kind = Kind::CHANNEL;
201 m_groupName = strGroupName;
202 m_groupClientID = iGroupClientID;
203 m_addonID = strAddonID;
204 m_instanceID = instanceID;
205 m_iChannelUID = iChannelUID;
206 m_path = StringUtils::Format("pvr://channels/{}/{}@{}/{}@{}_{}.pvr", bRadio ? "radio" : "tv",
207 CURL::Encode(m_groupName), m_groupClientID, m_instanceID,
208 m_addonID, m_iChannelUID);
209 }
210 }
211
212 bool CPVRChannelsPath::IsHiddenChannelGroup() const
213 {
214 return m_kind == Kind::GROUP && m_groupName == ".hidden";
215 }
216
217 std::string CPVRChannelsPath::TrimSlashes(const std::string& strString)
218 {
219 std::string strTrimmed = strString;
220 while (!strTrimmed.empty() && strTrimmed.front() == '/')
221 strTrimmed.erase(0, 1);
222
223 while (!strTrimmed.empty() && strTrimmed.back() == '/')
224 strTrimmed.pop_back();
225
226 return strTrimmed;
227 }