2 * Copyright (C) 2012-2013 Team XBMC
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)
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.
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/>.
21 #include "PVRChannelGroupsContainer.h"
23 #include "dialogs/GUIDialogOK.h"
24 #include "guilib/LocalizeStrings.h"
25 #include "utils/StringUtils.h"
26 #include "utils/URIUtils.h"
27 #include "utils/log.h"
28 #include "pvr/PVRManager.h"
29 #include "utils/StringUtils.h"
33 CPVRChannelGroupsContainer::CPVRChannelGroupsContainer(void) :
34 m_groupsRadio(new CPVRChannelGroups(true)),
35 m_groupsTV(new CPVRChannelGroups(false)),
36 m_bUpdateChannelsOnly(false),
41 CPVRChannelGroupsContainer::~CPVRChannelGroupsContainer(void)
47 bool CPVRChannelGroupsContainer::Update(bool bChannelsOnly /* = false */)
49 CSingleLock lock(m_critSection);
53 m_bUpdateChannelsOnly = bChannelsOnly;
56 CLog::Log(LOGDEBUG, "CPVRChannelGroupsContainer - %s - updating %s", __FUNCTION__, bChannelsOnly ? "channels" : "channel groups");
57 bool bReturn = m_groupsRadio->Update(bChannelsOnly) &&
58 m_groupsTV->Update(bChannelsOnly);
61 m_bIsUpdating = false;
67 bool CPVRChannelGroupsContainer::Load(void)
71 return m_groupsRadio->Load() &&
75 void CPVRChannelGroupsContainer::Unload(void)
77 m_groupsRadio->Clear();
81 CPVRChannelGroups *CPVRChannelGroupsContainer::Get(bool bRadio) const
83 return bRadio ? m_groupsRadio : m_groupsTV;
86 CPVRChannelGroupPtr CPVRChannelGroupsContainer::GetGroupAll(bool bRadio) const
88 return Get(bRadio)->GetGroupAll();
91 CPVRChannelGroupPtr CPVRChannelGroupsContainer::GetByIdFromAll(int iGroupId) const
93 CPVRChannelGroupPtr group = m_groupsTV->GetById(iGroupId);
95 group = m_groupsRadio->GetById(iGroupId);
100 CPVRChannelPtr CPVRChannelGroupsContainer::GetChannelById(int iChannelId) const
102 CPVRChannelPtr channel = m_groupsTV->GetGroupAll()->GetByChannelID(iChannelId);
104 channel = m_groupsRadio->GetGroupAll()->GetByChannelID(iChannelId);
109 CPVRChannelPtr CPVRChannelGroupsContainer::GetChannelByEpgId(int iEpgId) const
111 CPVRChannelPtr channel = m_groupsTV->GetGroupAll()->GetByChannelEpgID(iEpgId);
113 channel = m_groupsRadio->GetGroupAll()->GetByChannelEpgID(iEpgId);
118 bool CPVRChannelGroupsContainer::GetGroupsDirectory(CFileItemList *results, bool bRadio)
120 const CPVRChannelGroups *channelGroups = Get(bRadio);
123 channelGroups->GetGroupList(results);
129 CFileItemPtr CPVRChannelGroupsContainer::GetByPath(const CStdString &strPath) const
131 for (unsigned int bRadio = 0; bRadio <= 1; bRadio++)
133 const CPVRChannelGroups *groups = Get(bRadio == 1);
134 CFileItemPtr retVal = groups->GetByPath(strPath);
135 if (retVal && retVal->HasPVRChannelInfoTag())
139 CFileItemPtr retVal(new CFileItem);
143 bool CPVRChannelGroupsContainer::GetDirectory(const CStdString& strPath, CFileItemList &results)
145 CStdString strBase(strPath);
147 /* get the filename from curl */
149 CStdString fileName = url.GetFileName();
150 URIUtils::RemoveSlashAtEnd(fileName);
152 if (fileName == "channels")
156 /* all tv channels */
157 item.reset(new CFileItem(strBase + "/tv/", true));
158 item->SetLabel(g_localizeStrings.Get(19020));
159 item->SetLabelPreformated(true);
162 /* all radio channels */
163 item.reset(new CFileItem(strBase + "/radio/", true));
164 item->SetLabel(g_localizeStrings.Get(19021));
165 item->SetLabelPreformated(true);
170 else if (fileName == "channels/tv")
172 return GetGroupsDirectory(&results, false);
174 else if (fileName == "channels/radio")
176 return GetGroupsDirectory(&results, true);
178 else if (StringUtils::StartsWith(fileName, "channels/tv/"))
180 CStdString strGroupName(fileName.substr(12));
181 URIUtils::RemoveSlashAtEnd(strGroupName);
182 CPVRChannelGroupPtr group = GetTV()->GetByName(strGroupName);
184 group = GetGroupAllTV();
186 group->GetMembers(results, !StringUtils::EndsWithNoCase(fileName, ".hidden"));
189 else if (StringUtils::StartsWith(fileName, "channels/radio/"))
191 CStdString strGroupName(fileName.substr(15));
192 URIUtils::RemoveSlashAtEnd(strGroupName);
193 CPVRChannelGroupPtr group = GetRadio()->GetByName(strGroupName);
195 group = GetGroupAllRadio();
197 group->GetMembers(results, !StringUtils::EndsWithNoCase(fileName, ".hidden"));
204 int CPVRChannelGroupsContainer::GetNumChannelsFromAll()
206 return GetGroupAllTV()->Size() + GetGroupAllRadio()->Size();
209 CPVRChannelGroupPtr CPVRChannelGroupsContainer::GetSelectedGroup(bool bRadio) const
211 return Get(bRadio)->GetSelectedGroup();
214 CPVRChannelPtr CPVRChannelGroupsContainer::GetByUniqueID(int iUniqueChannelId, int iClientID)
216 CPVRChannelPtr channel;
217 CPVRChannelGroupPtr channelgroup = GetGroupAllTV();
219 channel = channelgroup->GetByClient(iUniqueChannelId, iClientID);
221 if (!channelgroup || !channel)
222 channelgroup = GetGroupAllRadio();
224 channel = channelgroup->GetByClient(iUniqueChannelId, iClientID);
229 CFileItemPtr CPVRChannelGroupsContainer::GetByChannelIDFromAll(int iChannelID)
231 CPVRChannelPtr channel;
232 CPVRChannelGroupPtr channelgroup = GetGroupAllTV();
234 channel = channelgroup->GetByChannelID(iChannelID);
238 channelgroup = GetGroupAllRadio();
240 channel = channelgroup->GetByChannelID(iChannelID);
245 CFileItemPtr retVal = CFileItemPtr(new CFileItem(*channel));
249 CFileItemPtr retVal = CFileItemPtr(new CFileItem);
253 void CPVRChannelGroupsContainer::SearchMissingChannelIcons(void)
255 CLog::Log(LOGINFO, "PVRChannelGroupsContainer - %s - starting channel icon search", __FUNCTION__);
257 // TODO: Add Process dialog here
258 CPVRChannelGroupPtr channelgrouptv = GetGroupAllTV();
259 CPVRChannelGroupPtr channelgroupradio = GetGroupAllRadio();
262 channelgrouptv->SearchAndSetChannelIcons(true);
263 if (channelgroupradio)
264 channelgroupradio->SearchAndSetChannelIcons(true);
266 CGUIDialogOK::ShowAndGetInput(19167,0,20177,0);
269 CFileItemPtr CPVRChannelGroupsContainer::GetLastPlayedChannel(void) const
271 CFileItemPtr lastChannel = GetGroupAllTV()->GetLastPlayedChannel();
272 bool bHasTVChannel(lastChannel && lastChannel->HasPVRChannelInfoTag());
274 CFileItemPtr lastRadioChannel = GetGroupAllRadio()->GetLastPlayedChannel();
275 bool bHasRadioChannel(lastRadioChannel && lastRadioChannel->HasPVRChannelInfoTag());
277 if (!bHasTVChannel || (bHasRadioChannel && lastChannel->GetPVRChannelInfoTag()->LastWatched() < lastRadioChannel->GetPVRChannelInfoTag()->LastWatched()))
278 return lastRadioChannel;
283 bool CPVRChannelGroupsContainer::CreateChannel(const CPVRChannel &channel)
285 return GetGroupAll(channel.IsRadio())->AddNewChannel(channel);
289 bool CPVRChannelGroupsContainer::CreateChannelEpgs(void)
291 return m_groupsRadio->CreateChannelEpgs() &&
292 m_groupsTV->CreateChannelEpgs();