[cosmetics] update date in GPL header
[vuplus_xbmc] / xbmc / pvr / windows / GUIWindowPVRSearch.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 "GUIWindowPVRSearch.h"
22
23 #include "dialogs/GUIDialogOK.h"
24 #include "dialogs/GUIDialogProgress.h"
25 #include "guilib/GUIWindowManager.h"
26 #include "pvr/PVRManager.h"
27 #include "pvr/channels/PVRChannelGroupsContainer.h"
28 #include "pvr/dialogs/GUIDialogPVRGuideSearch.h"
29 #include "epg/EpgContainer.h"
30 #include "pvr/recordings/PVRRecordings.h"
31 #include "GUIWindowPVR.h"
32 #include "utils/log.h"
33 #include "pvr/addons/PVRClients.h"
34
35 using namespace PVR;
36 using namespace EPG;
37
38 CGUIWindowPVRSearch::CGUIWindowPVRSearch(CGUIWindowPVR *parent) :
39   CGUIWindowPVRCommon(parent, PVR_WINDOW_SEARCH, CONTROL_BTNSEARCH, CONTROL_LIST_SEARCH),
40   m_bSearchStarted(false),
41   m_bSearchConfirmed(false)
42 {
43 }
44
45 void CGUIWindowPVRSearch::GetContextButtons(int itemNumber, CContextButtons &buttons) const
46 {
47   if (itemNumber < 0 || itemNumber >= m_parent->m_vecItems->Size())
48     return;
49   CFileItemPtr pItem = m_parent->m_vecItems->Get(itemNumber);
50
51   if (pItem->GetLabel() != g_localizeStrings.Get(19027))
52   {
53     if (pItem->GetEPGInfoTag()->EndAsLocalTime() > CDateTime::GetCurrentDateTime())
54     {
55       if (!pItem->GetEPGInfoTag()->HasTimer())
56       {
57         if (pItem->GetEPGInfoTag()->StartAsLocalTime() < CDateTime::GetCurrentDateTime())
58           buttons.Add(CONTEXT_BUTTON_START_RECORD, 264);   /* RECORD programme */
59         else
60           buttons.Add(CONTEXT_BUTTON_START_RECORD, 19061); /* Create a Timer */
61       }
62       else
63       {
64         if (pItem->GetEPGInfoTag()->StartAsLocalTime() < CDateTime::GetCurrentDateTime())
65           buttons.Add(CONTEXT_BUTTON_STOP_RECORD, 19059); /* Stop recording */
66         else
67           buttons.Add(CONTEXT_BUTTON_STOP_RECORD, 19060); /* Delete Timer */
68       }
69     }
70
71     buttons.Add(CONTEXT_BUTTON_INFO, 19047);              /* Epg info button */
72     buttons.Add(CONTEXT_BUTTON_SORTBY_CHANNEL, 19062);    /* Sort by channel */
73     buttons.Add(CONTEXT_BUTTON_SORTBY_NAME, 103);         /* Sort by Name */
74     buttons.Add(CONTEXT_BUTTON_SORTBY_DATE, 104);         /* Sort by Date */
75     buttons.Add(CONTEXT_BUTTON_CLEAR, 19232);             /* Clear search results */
76     if (pItem->GetEPGInfoTag()->HasPVRChannel() &&
77         g_PVRClients->HasMenuHooks(pItem->GetEPGInfoTag()->ChannelTag()->ClientID(), PVR_MENUHOOK_EPG))
78       buttons.Add(CONTEXT_BUTTON_MENU_HOOKS, 19195);      /* PVR client specific action */
79   }
80 }
81
82 bool CGUIWindowPVRSearch::OnContextButton(int itemNumber, CONTEXT_BUTTON button)
83 {
84   if (itemNumber < 0 || itemNumber >= m_parent->m_vecItems->Size())
85     return false;
86   CFileItemPtr pItem = m_parent->m_vecItems->Get(itemNumber);
87
88   return OnContextButtonClear(pItem.get(), button) ||
89       OnContextButtonInfo(pItem.get(), button) ||
90       OnContextButtonStopRecord(pItem.get(), button) ||
91       OnContextButtonStartRecord(pItem.get(), button) ||
92       CGUIWindowPVRCommon::OnContextButton(itemNumber, button);
93 }
94
95 void CGUIWindowPVRSearch::UpdateData(bool bUpdateSelectedFile /* = true */)
96 {
97   CLog::Log(LOGDEBUG, "CGUIWindowPVRSearch - %s - update window '%s'. set view to %d", __FUNCTION__, GetName(), m_iControlList);
98   m_bUpdateRequired = false;
99
100   /* lock the graphics context while updating */
101   CSingleLock graphicsLock(g_graphicsContext);
102
103   m_iSelected = m_parent->m_viewControl.GetSelectedItem();
104   m_parent->m_viewControl.Clear();
105   m_parent->m_vecItems->Clear();
106   m_parent->m_viewControl.SetCurrentView(m_iControlList);
107
108   if (m_bSearchConfirmed)
109   {
110     CGUIDialogProgress* dlgProgress = (CGUIDialogProgress*)g_windowManager.GetWindow(WINDOW_DIALOG_PROGRESS);
111     if (dlgProgress)
112     {
113       dlgProgress->SetHeading(194);
114       dlgProgress->SetLine(0, m_searchfilter.m_strSearchTerm);
115       dlgProgress->SetLine(1, "");
116       dlgProgress->SetLine(2, "");
117       dlgProgress->StartModal();
118       dlgProgress->Progress();
119     }
120
121     // TODO get this from the selected channel group
122     g_EpgContainer.GetEPGSearch(*m_parent->m_vecItems, m_searchfilter);
123     if (dlgProgress)
124       dlgProgress->Close();
125
126     if (m_parent->m_vecItems->Size() == 0)
127     {
128       CGUIDialogOK::ShowAndGetInput(194, 284, 0, 0);
129       m_bSearchConfirmed = false;
130     }
131   }
132
133   if (m_parent->m_vecItems->Size() == 0)
134   {
135     CFileItemPtr item;
136     item.reset(new CFileItem("pvr://guide/searchresults/empty.epg", false));
137     item->SetLabel(g_localizeStrings.Get(19027));
138     item->SetLabelPreformated(true);
139     m_parent->m_vecItems->Add(item);
140   }
141   else
142   {
143     m_parent->m_vecItems->Sort(m_iSortMethod, m_iSortOrder);
144   }
145
146   m_parent->m_viewControl.SetItems(*m_parent->m_vecItems);
147
148   if (bUpdateSelectedFile)
149     m_parent->m_viewControl.SetSelectedItem(m_iSelected);
150
151   m_parent->SetLabel(CONTROL_LABELHEADER, g_localizeStrings.Get(283));
152   m_parent->SetLabel(CONTROL_LABELGROUP, "");
153 }
154
155 bool CGUIWindowPVRSearch::OnClickButton(CGUIMessage &message)
156 {
157   bool bReturn = false;
158
159   if (IsSelectedButton(message))
160   {
161     bReturn = true;
162     ShowSearchResults();
163   }
164
165   return bReturn;
166 }
167
168 bool CGUIWindowPVRSearch::OnClickList(CGUIMessage &message)
169 {
170   bool bReturn = false;
171
172   if (IsSelectedList(message))
173   {
174     bReturn = true;
175     int iAction = message.GetParam1();
176     int iItem = m_parent->m_viewControl.GetSelectedItem();
177
178     /* get the fileitem pointer */
179     if (iItem < 0 || iItem >= m_parent->m_vecItems->Size())
180       return bReturn;
181     CFileItemPtr pItem = m_parent->m_vecItems->Get(iItem);
182
183     /* process actions */
184     if (iAction == ACTION_SHOW_INFO || iAction == ACTION_SELECT_ITEM || iAction == ACTION_MOUSE_LEFT_CLICK)
185       ActionShowSearch(pItem.get());
186     else if (iAction == ACTION_CONTEXT_MENU || iAction == ACTION_MOUSE_RIGHT_CLICK)
187       m_parent->OnPopupMenu(iItem);
188     else if (iAction == ACTION_RECORD)
189       ActionRecord(pItem.get());
190   }
191
192   return bReturn;
193 }
194
195 bool CGUIWindowPVRSearch::OnContextButtonClear(CFileItem *item, CONTEXT_BUTTON button)
196 {
197   bool bReturn = false;
198
199   if (button == CONTEXT_BUTTON_CLEAR)
200   {
201     bReturn = true;
202
203     m_bSearchStarted = false;
204     m_bSearchConfirmed = false;
205     m_searchfilter.Reset();
206
207     UpdateData();
208   }
209
210   return bReturn;
211 }
212
213 bool CGUIWindowPVRSearch::OnContextButtonInfo(CFileItem *item, CONTEXT_BUTTON button)
214 {
215   bool bReturn = false;
216
217   if (button == CONTEXT_BUTTON_INFO)
218   {
219     bReturn = true;
220
221     ShowEPGInfo(item);
222   }
223
224   return bReturn;
225 }
226
227 bool CGUIWindowPVRSearch::OnContextButtonStartRecord(CFileItem *item, CONTEXT_BUTTON button)
228 {
229   bool bReturn = false;
230
231   if (button == CONTEXT_BUTTON_START_RECORD)
232   {
233     bReturn = true;
234
235     StartRecordFile(item);
236   }
237
238   return bReturn;
239 }
240
241 bool CGUIWindowPVRSearch::OnContextButtonStopRecord(CFileItem *item, CONTEXT_BUTTON button)
242 {
243   bool bReturn = false;
244
245   if (button == CONTEXT_BUTTON_STOP_RECORD)
246   {
247     bReturn = true;
248
249     StopRecordFile(item);
250   }
251
252   return bReturn;
253 }
254
255 bool CGUIWindowPVRSearch::ActionShowSearch(CFileItem *item)
256 {
257   if (item->GetPath() == "pvr://guide/searchresults/empty.epg")
258     ShowSearchResults();
259   else
260     ShowEPGInfo(item);
261
262   return true;
263 }
264
265 void CGUIWindowPVRSearch::ShowSearchResults()
266 {
267   /* Load timer settings dialog */
268   CGUIDialogPVRGuideSearch* pDlgInfo = (CGUIDialogPVRGuideSearch*)g_windowManager.GetWindow(WINDOW_DIALOG_PVR_GUIDE_SEARCH);
269
270   if (!pDlgInfo)
271     return;
272
273   if (!m_bSearchStarted)
274   {
275     m_bSearchStarted = true;
276     m_searchfilter.Reset();
277   }
278
279   pDlgInfo->SetFilterData(&m_searchfilter);
280
281   /* Open dialog window */
282   pDlgInfo->DoModal();
283
284   if (pDlgInfo->IsConfirmed())
285   {
286     m_bSearchConfirmed = true;
287     UpdateData();
288   }
289 }