[cstdstring] demise Format, replacing with StringUtils::Format
[vuplus_xbmc] / xbmc / pvr / dialogs / GUIDialogPVRGuideSearch.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 "GUIDialogPVRGuideSearch.h"
22 #include "Application.h"
23 #include "guilib/LocalizeStrings.h"
24 #include "guilib/GUIEditControl.h"
25 #include "guilib/GUIRadioButtonControl.h"
26 #include "guilib/GUISpinControlEx.h"
27 #include "guilib/GUIWindowManager.h"
28 #include "utils/StringUtils.h"
29
30 #include "addons/include/xbmc_pvr_types.h"
31 #include "pvr/PVRManager.h"
32 #include "epg/EpgSearchFilter.h"
33 #include "pvr/channels/PVRChannelGroupsContainer.h"
34
35 using namespace std;
36 using namespace PVR;
37
38 #define CONTROL_EDIT_SEARCH       9
39 #define CONTROL_BTN_INC_DESC      10
40 #define CONTROL_BTN_CASE_SENS     11
41 #define CONTROL_SPIN_MIN_DURATION 12
42 #define CONTROL_SPIN_MAX_DURATION 13
43 #define CONTROL_EDIT_START_DATE   14
44 #define CONTROL_EDIT_STOP_DATE    15
45 #define CONTROL_EDIT_START_TIME   16
46 #define CONTROL_EDIT_STOP_TIME    17
47 #define CONTROL_SPIN_GENRE        18
48 #define CONTROL_SPIN_NO_REPEATS   19
49 #define CONTROL_BTN_UNK_GENRE     20
50 #define CONTROL_SPIN_GROUPS       21
51 #define CONTROL_BTN_FTA_ONLY      22
52 #define CONTROL_SPIN_CHANNELS     23
53 #define CONTROL_BTN_IGNORE_TMR    24
54 #define CONTROL_BTN_CANCEL        25
55 #define CONTROL_BTN_SEARCH        26
56 #define CONTROL_BTN_IGNORE_REC    27
57 #define CONTROL_BTN_DEFAULTS      28
58
59 CGUIDialogPVRGuideSearch::CGUIDialogPVRGuideSearch(void) :
60     CGUIDialog(WINDOW_DIALOG_PVR_GUIDE_SEARCH, "DialogPVRGuideSearch.xml"),
61     m_bConfirmed(false),
62     m_bCanceled(false),
63     m_searchFilter(NULL)
64 {
65 }
66
67 void CGUIDialogPVRGuideSearch::UpdateChannelSpin(void)
68 {
69   CGUISpinControlEx *pSpin = (CGUISpinControlEx *)GetControl(CONTROL_SPIN_CHANNELS);
70   CGUISpinControlEx *pSpinGroups = (CGUISpinControlEx *)GetControl(CONTROL_SPIN_GROUPS);
71   if (!pSpin || !pSpinGroups)
72     return;
73
74   int iChannelGroup = pSpin->GetValue();
75
76   pSpin->Clear();
77   pSpin->AddLabel(g_localizeStrings.Get(19217), EPG_SEARCH_UNSET);
78
79   int iGroupId = (iChannelGroup == EPG_SEARCH_UNSET) ?
80       XBMC_INTERNAL_GROUP_TV :
81       iChannelGroup;
82   CPVRChannelGroupPtr group = g_PVRChannelGroups->GetByIdFromAll(iGroupId);
83   if (!group)
84     group = g_PVRChannelGroups->GetGroupAllTV();
85
86   for (int iChannelPtr = 0; iChannelPtr < group->Size(); iChannelPtr++)
87   {
88     CFileItemPtr channel = group->GetByIndex(iChannelPtr);
89     if (!channel || !channel->HasPVRChannelInfoTag())
90       continue;
91
92     int iChannelNumber = group->GetChannelNumber(*channel->GetPVRChannelInfoTag());
93     pSpin->AddLabel(channel->GetPVRChannelInfoTag()->ChannelName().c_str(), iChannelNumber);
94   }
95 }
96
97 void CGUIDialogPVRGuideSearch::UpdateGroupsSpin(void)
98 {
99   CGUISpinControlEx *pSpin = (CGUISpinControlEx *)GetControl(CONTROL_SPIN_GROUPS);
100   if (!pSpin)
101     return;
102
103   std::vector<CPVRChannelGroupPtr> group;
104   std::vector<CPVRChannelGroupPtr>::const_iterator it;
105
106   /* tv groups */
107   group = g_PVRChannelGroups->GetTV()->GetMembers();
108   for (it = group.begin(); it != group.end(); ++it)
109     pSpin->AddLabel((*it)->GroupName(), (*it)->GroupID());
110
111   /* radio groups */
112   group = g_PVRChannelGroups->GetRadio()->GetMembers();
113   for (it = group.begin(); it != group.end(); ++it)
114     pSpin->AddLabel((*it)->GroupName(), (*it)->GroupID());
115
116   pSpin->SetValue(m_searchFilter->m_iChannelGroup);
117 }
118
119 void CGUIDialogPVRGuideSearch::UpdateGenreSpin(void)
120 {
121   CGUISpinControlEx *pSpin = (CGUISpinControlEx *)GetControl(CONTROL_SPIN_GENRE);
122   if (!pSpin)
123     return;
124
125   pSpin->Clear();
126   pSpin->AddLabel(g_localizeStrings.Get(593), EPG_SEARCH_UNSET);
127   pSpin->AddLabel(g_localizeStrings.Get(19500), EPG_EVENT_CONTENTMASK_MOVIEDRAMA);
128   pSpin->AddLabel(g_localizeStrings.Get(19516), EPG_EVENT_CONTENTMASK_NEWSCURRENTAFFAIRS);
129   pSpin->AddLabel(g_localizeStrings.Get(19532), EPG_EVENT_CONTENTMASK_SHOW);
130   pSpin->AddLabel(g_localizeStrings.Get(19548), EPG_EVENT_CONTENTMASK_SPORTS);
131   pSpin->AddLabel(g_localizeStrings.Get(19564), EPG_EVENT_CONTENTMASK_CHILDRENYOUTH);
132   pSpin->AddLabel(g_localizeStrings.Get(19580), EPG_EVENT_CONTENTMASK_MUSICBALLETDANCE);
133   pSpin->AddLabel(g_localizeStrings.Get(19596), EPG_EVENT_CONTENTMASK_ARTSCULTURE);
134   pSpin->AddLabel(g_localizeStrings.Get(19612), EPG_EVENT_CONTENTMASK_SOCIALPOLITICALECONOMICS);
135   pSpin->AddLabel(g_localizeStrings.Get(19628), EPG_EVENT_CONTENTMASK_EDUCATIONALSCIENCE);
136   pSpin->AddLabel(g_localizeStrings.Get(19644), EPG_EVENT_CONTENTMASK_LEISUREHOBBIES);
137   pSpin->AddLabel(g_localizeStrings.Get(19660), EPG_EVENT_CONTENTMASK_SPECIAL);
138   pSpin->AddLabel(g_localizeStrings.Get(19499), EPG_EVENT_CONTENTMASK_USERDEFINED);
139   pSpin->SetValue(m_searchFilter->m_iGenreType);
140 }
141
142 void CGUIDialogPVRGuideSearch::UpdateDurationSpin(void)
143 {
144   /* minimum duration */
145   CGUISpinControlEx *pSpin = (CGUISpinControlEx *)GetControl(CONTROL_SPIN_MIN_DURATION);
146   if (!pSpin)
147     return;
148
149   pSpin->Clear();
150   pSpin->AddLabel("-", EPG_SEARCH_UNSET);
151   for (int i = 1; i < 12*60/5; i++)
152   {
153     CStdString string;
154     string = StringUtils::Format(g_localizeStrings.Get(14044), i*5);
155     pSpin->AddLabel(string, i*5);
156   }
157   pSpin->SetValue(m_searchFilter->m_iMinimumDuration);
158
159   /* maximum duration */
160   pSpin = (CGUISpinControlEx *)GetControl(CONTROL_SPIN_MAX_DURATION);
161   if (!pSpin)
162     return;
163
164   pSpin->Clear();
165   pSpin->AddLabel("-", EPG_SEARCH_UNSET);
166   for (int i = 1; i < 12*60/5; i++)
167   {
168     CStdString string;
169     string = StringUtils::Format(g_localizeStrings.Get(14044), i*5);
170     pSpin->AddLabel(string, i*5);
171   }
172   pSpin->SetValue(m_searchFilter->m_iMaximumDuration);
173 }
174
175 bool CGUIDialogPVRGuideSearch::OnMessage(CGUIMessage& message)
176 {
177   CGUIDialog::OnMessage(message);
178
179   switch (message.GetMessage())
180   {
181     case GUI_MSG_CLICKED:
182     {
183       int iControl = message.GetSenderId();
184       if (iControl == CONTROL_BTN_SEARCH)
185       {
186         OnSearch();
187         m_bConfirmed = true;
188         m_bCanceled = false;
189         Close();
190         return true;
191       }
192       else if (iControl == CONTROL_BTN_CANCEL)
193       {
194         Close();
195         m_bCanceled = true;
196         return true;
197       }
198       else if (iControl == CONTROL_BTN_DEFAULTS)
199       {
200         if (m_searchFilter)
201         {
202           m_searchFilter->Reset();
203           Update();
204         }
205
206         return true;
207       }
208       else if (iControl == CONTROL_SPIN_GROUPS)
209       {
210         UpdateChannelSpin();
211         return true;
212       }
213     }
214     break;
215   }
216
217   return false;
218 }
219
220 void CGUIDialogPVRGuideSearch::OnInitWindow()
221 {
222   CGUIDialog::OnInitWindow();
223
224   m_bConfirmed = false;
225   m_bCanceled = false;
226 }
227
228 void CGUIDialogPVRGuideSearch::OnWindowLoaded()
229 {
230   Update();
231   return CGUIDialog::OnWindowLoaded();
232 }
233
234 void CGUIDialogPVRGuideSearch::ReadDateTime(const CStdString &strDate, const CStdString &strTime, CDateTime &dateTime) const
235 {
236   int iHours, iMinutes;
237   sscanf(strTime, "%d:%d", &iHours, &iMinutes);
238   dateTime.SetFromDBDate(strDate);
239   dateTime.SetDateTime(dateTime.GetYear(), dateTime.GetMonth(), dateTime.GetDay(), iHours, iMinutes, 0);
240 }
241
242 void CGUIDialogPVRGuideSearch::OnSearch()
243 {
244   CStdString              strTmp;
245   CGUISpinControlEx      *pSpin;
246   CGUIEditControl        *pEdit;
247   CGUIRadioButtonControl *pRadioButton;
248
249   if (!m_searchFilter)
250     return;
251
252   pEdit = (CGUIEditControl *)GetControl(CONTROL_EDIT_SEARCH);
253   if (pEdit) m_searchFilter->m_strSearchTerm = pEdit->GetLabel2();
254
255   pRadioButton = (CGUIRadioButtonControl *)GetControl(CONTROL_BTN_INC_DESC);
256   if (pRadioButton) m_searchFilter->m_bSearchInDescription = pRadioButton->IsSelected();
257
258   pRadioButton = (CGUIRadioButtonControl *)GetControl(CONTROL_BTN_CASE_SENS);
259   if (pRadioButton) m_searchFilter->m_bIsCaseSensitive = pRadioButton->IsSelected();
260
261   pRadioButton = (CGUIRadioButtonControl *)GetControl(CONTROL_BTN_FTA_ONLY);
262   if (pRadioButton) m_searchFilter->m_bFTAOnly = pRadioButton->IsSelected();
263
264   pRadioButton = (CGUIRadioButtonControl *)GetControl(CONTROL_BTN_UNK_GENRE);
265   if (pRadioButton) m_searchFilter->m_bIncludeUnknownGenres = pRadioButton->IsSelected();
266
267   pRadioButton = (CGUIRadioButtonControl *)GetControl(CONTROL_BTN_IGNORE_REC);
268   if (pRadioButton) m_searchFilter->m_bIgnorePresentRecordings = pRadioButton->IsSelected();
269
270   pRadioButton = (CGUIRadioButtonControl *)GetControl(CONTROL_BTN_IGNORE_TMR);
271   if (pRadioButton) m_searchFilter->m_bIgnorePresentTimers = pRadioButton->IsSelected();
272
273   pRadioButton = (CGUIRadioButtonControl *)GetControl(CONTROL_SPIN_NO_REPEATS);
274   if (pRadioButton) m_searchFilter->m_bPreventRepeats = pRadioButton->IsSelected();
275
276   pSpin = (CGUISpinControlEx *)GetControl(CONTROL_SPIN_GENRE);
277   if (pSpin) m_searchFilter->m_iGenreType = pSpin->GetValue();
278
279   pSpin = (CGUISpinControlEx *)GetControl(CONTROL_SPIN_MIN_DURATION);
280   if (pSpin) m_searchFilter->m_iMinimumDuration = pSpin->GetValue();
281
282   pSpin = (CGUISpinControlEx *)GetControl(CONTROL_SPIN_MAX_DURATION);
283   if (pSpin) m_searchFilter->m_iMaximumDuration = pSpin->GetValue();
284
285   pSpin = (CGUISpinControlEx *)GetControl(CONTROL_SPIN_CHANNELS);
286   if (pSpin) m_searchFilter->m_iChannelNumber = pSpin->GetValue();
287
288   pSpin = (CGUISpinControlEx *)GetControl(CONTROL_SPIN_GROUPS);
289   if (pSpin) m_searchFilter->m_iChannelGroup = pSpin->GetValue();
290
291   pEdit = (CGUIEditControl *)GetControl(CONTROL_EDIT_START_TIME);
292   if (pEdit) strTmp = pEdit->GetLabel2();
293
294   pEdit = (CGUIEditControl *)GetControl(CONTROL_EDIT_START_DATE);
295   if (pEdit) ReadDateTime(pEdit->GetLabel2(), strTmp, m_searchFilter->m_startDateTime);
296   strTmp.clear();
297
298   pEdit = (CGUIEditControl *)GetControl(CONTROL_EDIT_STOP_TIME);
299   if (pEdit) strTmp = pEdit->GetLabel2();
300
301   pEdit = (CGUIEditControl *)GetControl(CONTROL_EDIT_STOP_DATE);
302   if (pEdit) ReadDateTime(pEdit->GetLabel2(), strTmp, m_searchFilter->m_endDateTime);
303 }
304
305 void CGUIDialogPVRGuideSearch::Update()
306 {
307   CGUIEditControl        *pEdit;
308   CGUIRadioButtonControl *pRadioButton;
309
310   if (!m_searchFilter)
311     return;
312
313   pEdit = (CGUIEditControl *)GetControl(CONTROL_EDIT_SEARCH);
314   if (pEdit)
315   {
316     pEdit->SetLabel2(m_searchFilter->m_strSearchTerm);
317     pEdit->SetInputType(CGUIEditControl::INPUT_TYPE_TEXT, 16017);
318   }
319
320   pRadioButton = (CGUIRadioButtonControl *)GetControl(CONTROL_BTN_CASE_SENS);
321   if (pRadioButton) pRadioButton->SetSelected(m_searchFilter->m_bIsCaseSensitive);
322
323   pRadioButton = (CGUIRadioButtonControl *)GetControl(CONTROL_BTN_INC_DESC);
324   if (pRadioButton) pRadioButton->SetSelected(m_searchFilter->m_bSearchInDescription);
325
326   pRadioButton = (CGUIRadioButtonControl *)GetControl(CONTROL_BTN_FTA_ONLY);
327   if (pRadioButton) pRadioButton->SetSelected(m_searchFilter->m_bFTAOnly);
328
329   pRadioButton = (CGUIRadioButtonControl *)GetControl(CONTROL_BTN_UNK_GENRE);
330   if (pRadioButton) pRadioButton->SetSelected(m_searchFilter->m_bIncludeUnknownGenres);
331
332   pRadioButton = (CGUIRadioButtonControl *)GetControl(CONTROL_BTN_IGNORE_REC);
333   if (pRadioButton) pRadioButton->SetSelected(m_searchFilter->m_bIgnorePresentRecordings);
334
335   pRadioButton = (CGUIRadioButtonControl *)GetControl(CONTROL_BTN_IGNORE_TMR);
336   if (pRadioButton) pRadioButton->SetSelected(m_searchFilter->m_bIgnorePresentTimers);
337
338   pRadioButton = (CGUIRadioButtonControl *)GetControl(CONTROL_SPIN_NO_REPEATS);
339   if (pRadioButton) pRadioButton->SetSelected(m_searchFilter->m_bPreventRepeats);
340
341   /* Set time fields */
342   pEdit = (CGUIEditControl *)GetControl(CONTROL_EDIT_START_TIME);
343   if (pEdit)
344   {
345     pEdit->SetLabel2(m_searchFilter->m_endDateTime.GetAsLocalizedTime("", false));
346     pEdit->SetInputType(CGUIEditControl::INPUT_TYPE_TIME, 14066);
347   }
348   pEdit = (CGUIEditControl *)GetControl(CONTROL_EDIT_STOP_TIME);
349   if (pEdit)
350   {
351     pEdit->SetLabel2(m_searchFilter->m_startDateTime.GetAsLocalizedTime("", false));
352     pEdit->SetInputType(CGUIEditControl::INPUT_TYPE_TIME, 14066);
353   }
354   pEdit = (CGUIEditControl *)GetControl(CONTROL_EDIT_START_DATE);
355   if (pEdit)
356   {
357     pEdit->SetLabel2(m_searchFilter->m_startDateTime.GetAsDBDate());
358     pEdit->SetInputType(CGUIEditControl::INPUT_TYPE_DATE, 14067);
359   }
360   pEdit = (CGUIEditControl *)GetControl(CONTROL_EDIT_STOP_DATE);
361   if (pEdit)
362   {
363     pEdit->SetLabel2(m_searchFilter->m_endDateTime.GetAsDBDate());
364     pEdit->SetInputType(CGUIEditControl::INPUT_TYPE_DATE, 14067);
365   }
366
367   UpdateDurationSpin();
368   UpdateGroupsSpin();
369   UpdateChannelSpin();
370   UpdateGenreSpin();
371 }