[release] version bump to 13.0 beta1
[vuplus_xbmc] / xbmc / pvr / channels / PVRChannelGroupsContainer.cpp
1 /*
2  *      Copyright (C) 2012-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 "PVRChannelGroupsContainer.h"
22 #include "URL.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"
30
31 using namespace PVR;
32
33 CPVRChannelGroupsContainer::CPVRChannelGroupsContainer(void) :
34     m_groupsRadio(new CPVRChannelGroups(true)),
35     m_groupsTV(new CPVRChannelGroups(false)),
36     m_bUpdateChannelsOnly(false),
37     m_bIsUpdating(false)
38 {
39 }
40
41 CPVRChannelGroupsContainer::~CPVRChannelGroupsContainer(void)
42 {
43   delete m_groupsRadio;
44   delete m_groupsTV;
45 }
46
47 bool CPVRChannelGroupsContainer::Update(bool bChannelsOnly /* = false */)
48 {
49   CSingleLock lock(m_critSection);
50   if (m_bIsUpdating)
51     return false;
52   m_bIsUpdating = true;
53   m_bUpdateChannelsOnly = bChannelsOnly;
54   lock.Leave();
55
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);
59
60   lock.Enter();
61   m_bIsUpdating = false;
62   lock.Leave();
63
64   return bReturn;
65 }
66
67 bool CPVRChannelGroupsContainer::Load(void)
68 {
69   Unload();
70
71   return m_groupsRadio->Load() &&
72          m_groupsTV->Load();
73 }
74
75 void CPVRChannelGroupsContainer::Unload(void)
76 {
77   m_groupsRadio->Clear();
78   m_groupsTV->Clear();
79 }
80
81 CPVRChannelGroups *CPVRChannelGroupsContainer::Get(bool bRadio) const
82 {
83   return bRadio ? m_groupsRadio : m_groupsTV;
84 }
85
86 CPVRChannelGroupPtr CPVRChannelGroupsContainer::GetGroupAll(bool bRadio) const
87 {
88   return Get(bRadio)->GetGroupAll();
89 }
90
91 CPVRChannelGroupPtr CPVRChannelGroupsContainer::GetByIdFromAll(int iGroupId) const
92 {
93   CPVRChannelGroupPtr group = m_groupsTV->GetById(iGroupId);
94   if (!group)
95     group = m_groupsRadio->GetById(iGroupId);
96
97   return group;
98 }
99
100 CPVRChannelPtr CPVRChannelGroupsContainer::GetChannelById(int iChannelId) const
101 {
102   CPVRChannelPtr channel = m_groupsTV->GetGroupAll()->GetByChannelID(iChannelId);
103   if (!channel)
104     channel = m_groupsRadio->GetGroupAll()->GetByChannelID(iChannelId);
105
106   return channel;
107 }
108
109 CPVRChannelPtr CPVRChannelGroupsContainer::GetChannelByEpgId(int iEpgId) const
110 {
111   CPVRChannelPtr channel = m_groupsTV->GetGroupAll()->GetByChannelEpgID(iEpgId);
112   if (!channel)
113     channel = m_groupsRadio->GetGroupAll()->GetByChannelEpgID(iEpgId);
114
115   return channel;
116 }
117
118 bool CPVRChannelGroupsContainer::GetGroupsDirectory(CFileItemList *results, bool bRadio)
119 {
120   const CPVRChannelGroups *channelGroups = Get(bRadio);
121   if (channelGroups)
122   {
123     channelGroups->GetGroupList(results);
124     return true;
125   }
126   return false;
127 }
128
129 CFileItemPtr CPVRChannelGroupsContainer::GetByPath(const CStdString &strPath) const
130 {
131   for (unsigned int bRadio = 0; bRadio <= 1; bRadio++)
132   {
133     const CPVRChannelGroups *groups = Get(bRadio == 1);
134     CFileItemPtr retVal = groups->GetByPath(strPath);
135     if (retVal && retVal->HasPVRChannelInfoTag())
136       return retVal;
137   }
138
139   CFileItemPtr retVal(new CFileItem);
140   return retVal;
141 }
142
143 bool CPVRChannelGroupsContainer::GetDirectory(const CStdString& strPath, CFileItemList &results)
144 {
145   CStdString strBase(strPath);
146
147   /* get the filename from curl */
148   CURL url(strPath);
149   CStdString fileName = url.GetFileName();
150   URIUtils::RemoveSlashAtEnd(fileName);
151
152   if (fileName == "channels")
153   {
154     CFileItemPtr item;
155
156     /* all tv channels */
157     item.reset(new CFileItem(strBase + "/tv/", true));
158     item->SetLabel(g_localizeStrings.Get(19020));
159     item->SetLabelPreformated(true);
160     results.Add(item);
161
162     /* all radio channels */
163     item.reset(new CFileItem(strBase + "/radio/", true));
164     item->SetLabel(g_localizeStrings.Get(19021));
165     item->SetLabelPreformated(true);
166     results.Add(item);
167
168     return true;
169   }
170   else if (fileName == "channels/tv")
171   {
172     return GetGroupsDirectory(&results, false);
173   }
174   else if (fileName == "channels/radio")
175   {
176     return GetGroupsDirectory(&results, true);
177   }
178   else if (StringUtils::StartsWith(fileName, "channels/tv/"))
179   {
180     CStdString strGroupName(fileName.substr(12));
181     URIUtils::RemoveSlashAtEnd(strGroupName);
182     CPVRChannelGroupPtr group = GetTV()->GetByName(strGroupName);
183     if (!group)
184       group = GetGroupAllTV();
185     if (group)
186       group->GetMembers(results, !StringUtils::EndsWithNoCase(fileName, ".hidden"));
187     return true;
188   }
189   else if (StringUtils::StartsWith(fileName, "channels/radio/"))
190   {
191     CStdString strGroupName(fileName.substr(15));
192     URIUtils::RemoveSlashAtEnd(strGroupName);
193     CPVRChannelGroupPtr group = GetRadio()->GetByName(strGroupName);
194     if (!group)
195       group = GetGroupAllRadio();
196     if (group)
197       group->GetMembers(results, !StringUtils::EndsWithNoCase(fileName, ".hidden"));
198     return true;
199   }
200
201   return false;
202 }
203
204 int CPVRChannelGroupsContainer::GetNumChannelsFromAll()
205 {
206   return GetGroupAllTV()->Size() + GetGroupAllRadio()->Size();
207 }
208
209 CPVRChannelGroupPtr CPVRChannelGroupsContainer::GetSelectedGroup(bool bRadio) const
210 {
211   return Get(bRadio)->GetSelectedGroup();
212 }
213
214 CPVRChannelPtr CPVRChannelGroupsContainer::GetByUniqueID(int iUniqueChannelId, int iClientID)
215 {
216   CPVRChannelPtr channel;
217   CPVRChannelGroupPtr channelgroup = GetGroupAllTV();
218   if (channelgroup)
219     channel = channelgroup->GetByClient(iUniqueChannelId, iClientID);
220
221   if (!channelgroup || !channel)
222     channelgroup = GetGroupAllRadio();
223   if (channelgroup)
224     channel = channelgroup->GetByClient(iUniqueChannelId, iClientID);
225
226   return channel;
227 }
228
229 CFileItemPtr CPVRChannelGroupsContainer::GetByChannelIDFromAll(int iChannelID)
230 {
231   CPVRChannelPtr channel;
232   CPVRChannelGroupPtr channelgroup = GetGroupAllTV();
233   if (channelgroup)
234     channel = channelgroup->GetByChannelID(iChannelID);
235
236   if (!channel)
237   {
238     channelgroup = GetGroupAllRadio();
239     if (channelgroup)
240       channel = channelgroup->GetByChannelID(iChannelID);
241   }
242
243   if (channel)
244   {
245     CFileItemPtr retVal = CFileItemPtr(new CFileItem(*channel));
246     return retVal;
247   }
248
249   CFileItemPtr retVal = CFileItemPtr(new CFileItem);
250   return retVal;
251 }
252
253 void CPVRChannelGroupsContainer::SearchMissingChannelIcons(void)
254 {
255   CLog::Log(LOGINFO, "PVRChannelGroupsContainer - %s - starting channel icon search", __FUNCTION__);
256
257   // TODO: Add Process dialog here
258   CPVRChannelGroupPtr channelgrouptv  = GetGroupAllTV();
259   CPVRChannelGroupPtr channelgroupradio = GetGroupAllRadio();
260
261   if (channelgrouptv)
262     channelgrouptv->SearchAndSetChannelIcons(true);
263   if (channelgroupradio)
264     channelgroupradio->SearchAndSetChannelIcons(true);
265
266   CGUIDialogOK::ShowAndGetInput(19167,0,20177,0);
267 }
268
269 CFileItemPtr CPVRChannelGroupsContainer::GetLastPlayedChannel(void) const
270 {
271   CFileItemPtr lastChannel = GetGroupAllTV()->GetLastPlayedChannel();
272   bool bHasTVChannel(lastChannel && lastChannel->HasPVRChannelInfoTag());
273
274   CFileItemPtr lastRadioChannel = GetGroupAllRadio()->GetLastPlayedChannel();
275   bool bHasRadioChannel(lastRadioChannel && lastRadioChannel->HasPVRChannelInfoTag());
276
277   if (!bHasTVChannel || (bHasRadioChannel && lastChannel->GetPVRChannelInfoTag()->LastWatched() < lastRadioChannel->GetPVRChannelInfoTag()->LastWatched()))
278     return lastRadioChannel;
279
280   return lastChannel;
281 }
282
283 bool CPVRChannelGroupsContainer::CreateChannel(const CPVRChannel &channel)
284 {
285   return GetGroupAll(channel.IsRadio())->AddNewChannel(channel);
286 }
287
288
289 bool CPVRChannelGroupsContainer::CreateChannelEpgs(void)
290 {
291   return m_groupsRadio->CreateChannelEpgs() &&
292          m_groupsTV->CreateChannelEpgs();
293 }