[pvr] channel group selection without channel switch, do not change
[vuplus_xbmc] / xbmc / pvr / dialogs / GUIDialogPVRChannelsOSD.cpp
1 /*
2  *      Copyright (C) 2012 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 "GUIDialogPVRChannelsOSD.h"
22 #include "Application.h"
23 #include "ApplicationMessenger.h"
24 #include "FileItem.h"
25 #include "guilib/GUIWindowManager.h"
26 #include "dialogs/GUIDialogOK.h"
27 #include "GUIDialogPVRGuideInfo.h"
28 #include "ViewState.h"
29 #include "settings/GUISettings.h"
30 #include "GUIInfoManager.h"
31
32 #include "pvr/PVRManager.h"
33 #include "pvr/channels/PVRChannelGroupsContainer.h"
34 #include "epg/Epg.h"
35 #include "pvr/timers/PVRTimerInfoTag.h"
36 #include "pvr/channels/PVRChannelGroupsContainer.h"
37
38 using namespace std;
39 using namespace PVR;
40 using namespace EPG;
41
42 #define CONTROL_LIST                  11
43
44 CGUIDialogPVRChannelsOSD::CGUIDialogPVRChannelsOSD() :
45     CGUIDialog(WINDOW_DIALOG_PVR_OSD_CHANNELS, "DialogPVRChannelsOSD.xml"),
46     Observer()
47 {
48   m_vecItems = new CFileItemList;
49 }
50
51 CGUIDialogPVRChannelsOSD::~CGUIDialogPVRChannelsOSD()
52 {
53   delete m_vecItems;
54
55   if (IsObserving(g_infoManager))
56     g_infoManager.UnregisterObserver(this);
57 }
58
59 bool CGUIDialogPVRChannelsOSD::OnMessage(CGUIMessage& message)
60 {
61   switch (message.GetMessage())
62   {
63   case GUI_MSG_WINDOW_DEINIT:
64     {
65       g_PVRManager.SetPlayingGroup(m_group);
66       Clear();
67     }
68     break;
69
70   case GUI_MSG_WINDOW_INIT:
71     {
72       /* Close dialog immediately if now TV or radio channel is playing */
73       if (!g_PVRManager.IsPlaying())
74       {
75         Close();
76         return true;
77       }
78
79       m_group = GetPlayingGroup();
80
81       CGUIWindow::OnMessage(message);
82       Update(true);
83
84       return true;
85     }
86     break;
87
88   case GUI_MSG_CLICKED:
89     {
90       int iControl = message.GetSenderId();
91
92       if (m_viewControl.HasControl(iControl))   // list/thumb control
93       {
94         int iItem = m_viewControl.GetSelectedItem();
95         int iAction = message.GetParam1();
96
97         if (iAction == ACTION_SELECT_ITEM || iAction == ACTION_MOUSE_LEFT_CLICK)
98         {
99           /* Switch to channel */
100           GotoChannel(iItem);
101           return true;
102         }
103         else if (iAction == ACTION_SHOW_INFO || iAction == ACTION_MOUSE_RIGHT_CLICK)
104         {
105           /* Show information Dialog */
106           ShowInfo(iItem);
107           return true;
108         }
109       }
110     }
111     break;
112
113   case GUI_MSG_MOVE:
114     {
115       int iAction = message.GetParam1();
116
117       if (iAction == ACTION_MOVE_RIGHT || iAction == ACTION_MOVE_LEFT)
118       {
119         CPVRChannelGroupPtr group = GetPlayingGroup();
120         CPVRChannelGroupPtr nextGroup = iAction == ACTION_MOVE_RIGHT ? group->GetNextGroup() : group->GetPreviousGroup();
121         g_PVRManager.SetPlayingGroup(nextGroup);
122
123         Clear();
124         Update();
125
126         return true;
127       }
128     }
129     break;
130   }
131
132   return CGUIDialog::OnMessage(message);
133 }
134
135 CPVRChannelGroupPtr CGUIDialogPVRChannelsOSD::GetPlayingGroup()
136 {
137   CPVRChannelPtr channel;
138   g_PVRManager.GetCurrentChannel(channel);
139   return g_PVRManager.GetPlayingGroup(channel->IsRadio());
140 }
141
142 void CGUIDialogPVRChannelsOSD::Update()
143 {
144   CGUIDialogPVRChannelsOSD::Update(false);
145 }
146
147 void CGUIDialogPVRChannelsOSD::Update(bool selectPlayingChannel)
148 {
149   // lock our display, as this window is rendered from the player thread
150   g_graphicsContext.Lock();
151
152   if (!IsObserving(g_infoManager))
153     g_infoManager.RegisterObserver(this);
154
155   m_viewControl.SetCurrentView(DEFAULT_VIEW_LIST);
156
157   // empty the list ready for population
158   Clear();
159
160   CPVRChannelPtr channel;
161   g_PVRManager.GetCurrentChannel(channel);
162   CPVRChannelGroupPtr group = g_PVRManager.GetPlayingGroup(channel->IsRadio());
163
164   if (group)
165   {
166     group->GetMembers(*m_vecItems);
167     m_viewControl.SetItems(*m_vecItems);
168     m_viewControl.SetSelectedItem(selectPlayingChannel ? group->GetIndex(*channel) : 0);
169   }
170
171   g_graphicsContext.Unlock();
172 }
173
174 void CGUIDialogPVRChannelsOSD::Clear()
175 {
176   m_viewControl.Clear();
177   m_vecItems->Clear();
178 }
179
180 void CGUIDialogPVRChannelsOSD::CloseOrSelect(unsigned int iItem)
181 {
182   if (g_guiSettings.GetBool("pvrmenu.closechannelosdonswitch"))
183     Close();
184   else
185     m_viewControl.SetSelectedItem(iItem);
186 }
187
188 void CGUIDialogPVRChannelsOSD::GotoChannel(int item)
189 {
190   /* Check file item is in list range and get his pointer */
191   if (item < 0 || item >= (int)m_vecItems->Size()) return;
192   CFileItemPtr pItem = m_vecItems->Get(item);
193
194   if (pItem->GetPath() == g_application.CurrentFile())
195   {
196     CloseOrSelect(item);
197     return;
198   }
199
200   if (g_PVRManager.IsPlaying() && pItem->HasPVRChannelInfoTag() && g_application.m_pPlayer)
201   {
202     CPVRChannel *channel = pItem->GetPVRChannelInfoTag();
203     if (!g_PVRManager.CheckParentalLock(*channel) ||
204         !g_application.m_pPlayer->SwitchChannel(*channel))
205     {
206       Close(true);
207       return;
208     }
209   }
210   else
211     CApplicationMessenger::Get().PlayFile(*pItem);
212
213   m_group = GetPlayingGroup();
214
215   CloseOrSelect(item);
216 }
217
218 void CGUIDialogPVRChannelsOSD::ShowInfo(int item)
219 {
220   /* Check file item is in list range and get his pointer */
221   if (item < 0 || item >= (int)m_vecItems->Size()) return;
222
223   CFileItemPtr pItem = m_vecItems->Get(item);
224   if (pItem && pItem->IsPVRChannel())
225   {
226     CPVRChannel *channel = pItem->GetPVRChannelInfoTag();
227     if (!g_PVRManager.CheckParentalLock(*channel))
228       return;
229
230     /* Get the current running show on this channel from the EPG storage */
231     CEpgInfoTag epgnow;
232     if (!channel->GetEPGNow(epgnow))
233       return;
234     CFileItem *itemNow  = new CFileItem(epgnow);
235
236     /* Load programme info dialog */
237     CGUIDialogPVRGuideInfo* pDlgInfo = (CGUIDialogPVRGuideInfo*)g_windowManager.GetWindow(WINDOW_DIALOG_PVR_GUIDE_INFO);
238     if (!pDlgInfo)
239       return;
240
241     /* inform dialog about the file item and open dialog window */
242     pDlgInfo->SetProgInfo(itemNow);
243     pDlgInfo->DoModal();
244     delete itemNow; /* delete previuosly created FileItem */
245   }
246
247   return;
248 }
249
250 void CGUIDialogPVRChannelsOSD::OnWindowLoaded()
251 {
252   CGUIDialog::OnWindowLoaded();
253   m_viewControl.Reset();
254   m_viewControl.SetParentWindow(GetID());
255   m_viewControl.AddView(GetControl(CONTROL_LIST));
256 }
257
258 void CGUIDialogPVRChannelsOSD::OnWindowUnload()
259 {
260   CGUIDialog::OnWindowUnload();
261   m_viewControl.Reset();
262 }
263
264 CGUIControl *CGUIDialogPVRChannelsOSD::GetFirstFocusableControl(int id)
265 {
266   if (m_viewControl.HasControl(id))
267     id = m_viewControl.GetCurrentControl();
268
269   return CGUIWindow::GetFirstFocusableControl(id);
270 }
271
272 void CGUIDialogPVRChannelsOSD::Notify(const Observable &obs, const ObservableMessage msg)
273 {
274   if (msg == ObservableMessageCurrentItem)
275   {
276     g_graphicsContext.Lock();
277     m_viewControl.SetItems(*m_vecItems);
278     g_graphicsContext.Unlock();
279   }
280 }