[cosmetics] update date in GPL header
[vuplus_xbmc] / xbmc / pvr / dialogs / GUIDialogPVRGuideInfo.cpp
1 /*
2  *      Copyright (C) 2012-2013 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 "GUIDialogPVRGuideInfo.h"
22 #include "Application.h"
23 #include "guilib/GUIWindowManager.h"
24 #include "dialogs/GUIDialogOK.h"
25 #include "dialogs/GUIDialogYesNo.h"
26
27 #include "pvr/PVRManager.h"
28 #include "pvr/channels/PVRChannelGroupsContainer.h"
29 #include "epg/EpgInfoTag.h"
30 #include "pvr/timers/PVRTimers.h"
31 #include "pvr/timers/PVRTimerInfoTag.h"
32
33 using namespace std;
34 using namespace PVR;
35 using namespace EPG;
36
37 #define CONTROL_BTN_SWITCH              5
38 #define CONTROL_BTN_RECORD              6
39 #define CONTROL_BTN_OK                  7
40
41 CGUIDialogPVRGuideInfo::CGUIDialogPVRGuideInfo(void)
42     : CGUIDialog(WINDOW_DIALOG_PVR_GUIDE_INFO, "DialogPVRGuideInfo.xml")
43     , m_progItem(new CFileItem)
44 {
45 }
46
47 CGUIDialogPVRGuideInfo::~CGUIDialogPVRGuideInfo(void)
48 {
49 }
50
51 bool CGUIDialogPVRGuideInfo::ActionStartTimer(const CEpgInfoTag *tag)
52 {
53   bool bReturn = false;
54
55   if (!tag)
56     return false;
57
58   CPVRChannelPtr channel = tag->ChannelTag();
59   if (!channel || !g_PVRManager.CheckParentalLock(*channel))
60     return false;
61
62   // prompt user for confirmation of channel record
63   CGUIDialogYesNo* pDialog = (CGUIDialogYesNo*)g_windowManager.GetWindow(WINDOW_DIALOG_YES_NO);
64
65   if (pDialog)
66   {
67     pDialog->SetHeading(264);
68     pDialog->SetLine(0, "");
69     pDialog->SetLine(1, tag->Title());
70     pDialog->SetLine(2, "");
71     pDialog->DoModal();
72
73     if (pDialog->IsConfirmed())
74     {
75       Close();
76       CPVRTimerInfoTag *newTimer = CPVRTimerInfoTag::CreateFromEpg(*tag);
77       if (newTimer)
78       {
79         bReturn = CPVRTimers::AddTimer(*newTimer);
80         delete newTimer;
81       }
82       else
83       {
84         bReturn = false;
85       }
86     }
87   }
88
89   return bReturn;
90 }
91
92 bool CGUIDialogPVRGuideInfo::ActionCancelTimer(CFileItemPtr timer)
93 {
94   bool bReturn = false;
95   if (!timer || !timer->HasPVRTimerInfoTag())
96   {
97     return bReturn;
98   }
99
100   // prompt user for confirmation of timer deletion
101   CGUIDialogYesNo* pDialog = (CGUIDialogYesNo*)g_windowManager.GetWindow(WINDOW_DIALOG_YES_NO);
102
103   if (pDialog)
104   {
105     pDialog->SetHeading(265);
106     pDialog->SetLine(0, "");
107     pDialog->SetLine(1, timer->GetPVRTimerInfoTag()->m_strTitle);
108     pDialog->SetLine(2, "");
109     pDialog->DoModal();
110
111     if (pDialog->IsConfirmed())
112     {
113       Close();
114       bReturn = CPVRTimers::DeleteTimer(*timer);
115     }
116   }
117
118   return bReturn;
119 }
120
121 bool CGUIDialogPVRGuideInfo::OnClickButtonOK(CGUIMessage &message)
122 {
123   bool bReturn = false;
124
125   if (message.GetSenderId() == CONTROL_BTN_OK)
126   {
127     Close();
128     bReturn = true;
129   }
130
131   return bReturn;
132 }
133
134 bool CGUIDialogPVRGuideInfo::OnClickButtonRecord(CGUIMessage &message)
135 {
136   bool bReturn = false;
137
138   if (message.GetSenderId() == CONTROL_BTN_RECORD)
139   {
140     bReturn = true;
141
142     const CEpgInfoTag *tag = m_progItem->GetEPGInfoTag();
143     if (!tag || !tag->HasPVRChannel())
144     {
145       /* invalid channel */
146       CGUIDialogOK::ShowAndGetInput(19033,19067,0,0);
147       Close();
148       return bReturn;
149     }
150
151     CFileItemPtr timerTag = g_PVRTimers->GetTimerForEpgTag(m_progItem.get());
152     bool bHasTimer = timerTag != NULL && timerTag->HasPVRTimerInfoTag();
153
154     if (!bHasTimer)
155       ActionStartTimer(tag);
156     else
157       ActionCancelTimer(timerTag);
158   }
159
160   return bReturn;
161 }
162
163 bool CGUIDialogPVRGuideInfo::OnClickButtonSwitch(CGUIMessage &message)
164 {
165   bool bReturn = false;
166
167   if (message.GetSenderId() == CONTROL_BTN_SWITCH)
168   {
169     Close();
170
171     if (!m_progItem->GetEPGInfoTag()->HasPVRChannel() ||
172         !g_application.PlayFile(CFileItem(*m_progItem->GetEPGInfoTag()->ChannelTag())))
173       CGUIDialogOK::ShowAndGetInput(19033,0,19035,0);
174     else
175       bReturn = true;
176   }
177
178   return bReturn;
179 }
180
181 bool CGUIDialogPVRGuideInfo::OnMessage(CGUIMessage& message)
182 {
183   switch (message.GetMessage())
184   {
185   case GUI_MSG_WINDOW_INIT:
186     CGUIDialog::OnMessage(message);
187     Update();
188     return true;
189   case GUI_MSG_CLICKED:
190     return OnClickButtonOK(message) ||
191            OnClickButtonRecord(message) ||
192            OnClickButtonSwitch(message);
193   }
194
195   return CGUIDialog::OnMessage(message);
196 }
197
198 void CGUIDialogPVRGuideInfo::SetProgInfo(const CFileItem *item)
199 {
200   *m_progItem = *item;
201 }
202
203 CFileItemPtr CGUIDialogPVRGuideInfo::GetCurrentListItem(int offset)
204 {
205   return m_progItem;
206 }
207
208 void CGUIDialogPVRGuideInfo::Update()
209 {
210   const CEpgInfoTag *tag = m_progItem->GetEPGInfoTag();
211   if (!tag)
212   {
213     /* no epg event selected */
214     return;
215   }
216
217   if (tag->EndAsLocalTime() <= CDateTime::GetCurrentDateTime())
218   {
219     /* event has passed. hide the record button */
220     SET_CONTROL_HIDDEN(CONTROL_BTN_RECORD);
221     return;
222   }
223
224   CFileItemPtr match = g_PVRTimers->GetTimerForEpgTag(m_progItem.get());
225   if (!match || !match->HasPVRTimerInfoTag())
226   {
227     /* no timer present on this tag */
228     if (tag->StartAsLocalTime() < CDateTime::GetCurrentDateTime())
229       SET_CONTROL_LABEL(CONTROL_BTN_RECORD, 264);
230     else
231       SET_CONTROL_LABEL(CONTROL_BTN_RECORD, 19061);
232   }
233   else
234   {
235     /* timer present on this tag */
236     if (tag->StartAsLocalTime() < CDateTime::GetCurrentDateTime())
237       SET_CONTROL_LABEL(CONTROL_BTN_RECORD, 19059);
238     else
239       SET_CONTROL_LABEL(CONTROL_BTN_RECORD, 19060);
240   }
241 }