2 * Copyright (C) 2012-2013 Team XBMC
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)
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.
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/>.
21 #include "GUIDialogPVRTimerSettings.h"
22 #include "guilib/GUIKeyboardFactory.h"
23 #include "dialogs/GUIDialogNumeric.h"
24 #include "settings/GUISettings.h"
25 #include "guilib/LocalizeStrings.h"
27 #include "pvr/PVRManager.h"
28 #include "pvr/timers/PVRTimerInfoTag.h"
29 #include "pvr/channels/PVRChannelGroupsContainer.h"
34 #define CONTROL_TMR_ACTIVE 20
35 #define CONTROL_TMR_CHNAME_TV 21
36 #define CONTROL_TMR_DAY 22
37 #define CONTROL_TMR_BEGIN 23
38 #define CONTROL_TMR_END 24
39 #define CONTROL_TMR_PRIORITY 26
40 #define CONTROL_TMR_LIFETIME 27
41 #define CONTROL_TMR_FIRST_DAY 28
42 #define CONTROL_TMR_NAME 29
43 #define CONTROL_TMR_DIR 30
44 #define CONTROL_TMR_RADIO 50
45 #define CONTROL_TMR_CHNAME_RADIO 51
47 CGUIDialogPVRTimerSettings::CGUIDialogPVRTimerSettings(void)
48 : CGUIDialogSettings(WINDOW_DIALOG_PVR_TIMER_SETTING, "DialogPVRTimerSettings.xml")
52 m_loadType = LOAD_EVERY_TIME;
55 void CGUIDialogPVRTimerSettings::AddChannelNames(CFileItemList &channelsList, SETTINGSTRINGS &channelNames, bool bRadio)
57 g_PVRChannelGroups->GetGroupAll(bRadio)->GetMembers(channelsList);
59 channelNames.push_back("0 dummy");
60 for (int i = 0; i < channelsList.Size(); i++)
63 CFileItemPtr item = channelsList[i];
64 const CPVRChannel *channel = item->GetPVRChannelInfoTag();
65 string.Format("%i %s", channel->ChannelNumber(), channel->ChannelName().c_str());
66 channelNames.push_back(string);
69 int iControl = bRadio ? CONTROL_TMR_CHNAME_RADIO : CONTROL_TMR_CHNAME_TV;
70 AddSpin(iControl, 19078, &m_timerItem->GetPVRTimerInfoTag()->m_iChannelNumber, channelNames.size(), channelNames);
71 EnableSettings(iControl, m_timerItem->GetPVRTimerInfoTag()->m_bIsRadio == bRadio);
74 void CGUIDialogPVRTimerSettings::SetWeekdaySettingFromTimer(const CPVRTimerInfoTag &timer)
76 if (timer.m_bIsRepeating)
78 if (timer.m_iWeekdays == 0x01)
80 else if (timer.m_iWeekdays == 0x02)
82 else if (timer.m_iWeekdays == 0x04)
84 else if (timer.m_iWeekdays == 0x08)
86 else if (timer.m_iWeekdays == 0x10)
88 else if (timer.m_iWeekdays == 0x20)
90 else if (timer.m_iWeekdays == 0x40)
92 else if (timer.m_iWeekdays == 0x1F)
94 else if (timer.m_iWeekdays == 0x3F)
96 else if (timer.m_iWeekdays == 0x7F)
98 else if (timer.m_iWeekdays == 0x60)
103 void CGUIDialogPVRTimerSettings::SetTimerFromWeekdaySetting(CPVRTimerInfoTag &timer)
105 timer.m_bIsRepeating = true;
108 timer.m_iWeekdays = 0x01;
109 else if (m_tmp_day == 1)
110 timer.m_iWeekdays = 0x02;
111 else if (m_tmp_day == 2)
112 timer.m_iWeekdays = 0x04;
113 else if (m_tmp_day == 3)
114 timer.m_iWeekdays = 0x08;
115 else if (m_tmp_day == 4)
116 timer.m_iWeekdays = 0x10;
117 else if (m_tmp_day == 5)
118 timer.m_iWeekdays = 0x20;
119 else if (m_tmp_day == 6)
120 timer.m_iWeekdays = 0x40;
121 else if (m_tmp_day == 7)
122 timer.m_iWeekdays = 0x1F;
123 else if (m_tmp_day == 8)
124 timer.m_iWeekdays = 0x3F;
125 else if (m_tmp_day == 9)
126 timer.m_iWeekdays = 0x7F;
127 else if (m_tmp_day == 10)
128 timer.m_iWeekdays = 0x60;
130 timer.m_iWeekdays = 0;
133 void CGUIDialogPVRTimerSettings::CreateSettings()
135 CPVRTimerInfoTag* tag = m_timerItem->GetPVRTimerInfoTag();
137 // clear out any old settings
140 // create our settings controls
141 m_bTimerActive = tag->IsActive();
142 AddBool(CONTROL_TMR_ACTIVE, 19074, &m_bTimerActive);
143 AddButton(CONTROL_TMR_NAME, 19075, &tag->m_strTitle, true);
145 if (tag->SupportsFolders())
146 AddButton(CONTROL_TMR_DIR, 19076, &tag->m_strDirectory, true);
148 AddBool(CONTROL_TMR_RADIO, 19077, &tag->m_bIsRadio);
153 CFileItemList channelslist_tv;
154 SETTINGSTRINGS channelstrings_tv;
155 AddChannelNames(channelslist_tv, channelstrings_tv, false);
158 CFileItemList channelslist_radio;
159 SETTINGSTRINGS channelstrings_radio;
160 AddChannelNames(channelslist_radio, channelstrings_radio, true);
165 SETTINGSTRINGS daystrings;
169 for (unsigned int iDayPtr = 19086; iDayPtr <= 19096; iDayPtr++)
170 daystrings.push_back(g_localizeStrings.Get(iDayPtr));
171 CDateTime time = CDateTime::GetCurrentDateTime();
172 CDateTime timestart = tag->StartAsLocalTime();
174 /* get diffence of timer in days between today and timer start date */
175 time.GetAsTm(time_cur);
176 timestart.GetAsTm(time_tmr);
178 m_tmp_day += time_tmr.tm_yday - time_cur.tm_yday;
179 if (time_tmr.tm_yday - time_cur.tm_yday < 0)
182 for (int i = 1; i < 365; ++i)
184 CStdString string = time.GetAsLocalizedDate();
185 daystrings.push_back(string);
186 time += CDateTimeSpan(1, 0, 0, 0);
189 SetWeekdaySettingFromTimer(*tag);
191 AddSpin(CONTROL_TMR_DAY, 19079, &m_tmp_day, daystrings.size(), daystrings);
194 AddButton(CONTROL_TMR_BEGIN, 19080, &timerStartTimeStr, true);
195 AddButton(CONTROL_TMR_END, 19081, &timerEndTimeStr, true);
196 AddSpin(CONTROL_TMR_PRIORITY, 19082, &tag->m_iPriority, 0, 99);
197 AddSpin(CONTROL_TMR_LIFETIME, 19083, &tag->m_iLifetime, 0, 365);
201 SETTINGSTRINGS daystrings;
205 CDateTime time = CDateTime::GetCurrentDateTime();
206 CDateTime timestart = tag->FirstDayAsLocalTime();
208 /* get diffence of timer in days between today and timer start date */
209 if (time < timestart)
211 time.GetAsTm(time_cur);
212 timestart.GetAsTm(time_tmr);
214 m_tmp_iFirstDay += time_tmr.tm_yday - time_cur.tm_yday + 1;
215 if (time_tmr.tm_yday - time_cur.tm_yday < 0)
216 m_tmp_iFirstDay += 365;
219 daystrings.push_back(g_localizeStrings.Get(19030));
221 for (int i = 1; i < 365; ++i)
223 CStdString string = time.GetAsLocalizedDate();
224 daystrings.push_back(string);
225 time += CDateTimeSpan(1, 0, 0, 0);
228 AddSpin(CONTROL_TMR_FIRST_DAY, 19084, &m_tmp_iFirstDay, daystrings.size(), daystrings);
230 if (tag->m_bIsRepeating)
231 EnableSettings(CONTROL_TMR_FIRST_DAY, true);
233 EnableSettings(CONTROL_TMR_FIRST_DAY, false);
237 void CGUIDialogPVRTimerSettings::OnSettingChanged(SettingInfo &setting)
239 CPVRTimerInfoTag* tag = m_timerItem->GetPVRTimerInfoTag();
241 if (setting.id == CONTROL_TMR_NAME)
243 if (CGUIKeyboardFactory::ShowAndGetInput(tag->m_strTitle, g_localizeStrings.Get(19097), false))
245 UpdateSetting(CONTROL_TMR_NAME);
248 if (setting.id == CONTROL_TMR_DIR && CGUIKeyboardFactory::ShowAndGetInput(tag->m_strDirectory, g_localizeStrings.Get(19104), false))
249 UpdateSetting(CONTROL_TMR_DIR);
250 else if (setting.id == CONTROL_TMR_RADIO || setting.id == CONTROL_TMR_CHNAME_TV || setting.id == CONTROL_TMR_CHNAME_RADIO)
252 if (setting.id == CONTROL_TMR_RADIO)
254 EnableSettings(CONTROL_TMR_CHNAME_TV, !tag->m_bIsRadio);
255 EnableSettings(CONTROL_TMR_CHNAME_RADIO, tag->m_bIsRadio);
258 CFileItemPtr channel = g_PVRChannelGroups->GetGroupAll(tag->m_bIsRadio)->GetByChannelNumber(tag->m_iChannelNumber);
259 if (channel && channel->HasPVRChannelInfoTag())
261 tag->m_iClientChannelUid = channel->GetPVRChannelInfoTag()->UniqueID();
262 tag->m_iClientId = channel->GetPVRChannelInfoTag()->ClientID();
263 tag->m_bIsRadio = channel->GetPVRChannelInfoTag()->IsRadio();
264 tag->m_iChannelNumber = channel->GetPVRChannelInfoTag()->ChannelNumber();
266 // Update channel pointer from above values
267 tag->UpdateChannel();
270 else if (setting.id == CONTROL_TMR_DAY && m_tmp_day > 10)
272 CDateTime time = CDateTime::GetCurrentDateTime();
273 CDateTime timestart = timerStartTime;
274 CDateTime timestop = timerEndTime;
279 /* get diffence of timer in days between today and timer start date */
280 time.GetAsTm(time_cur);
281 timestart.GetAsTm(time_tmr);
283 m_tmp_diff = time_tmr.tm_yday - time_cur.tm_yday;
284 if (time_tmr.tm_yday - time_cur.tm_yday < 0)
287 CDateTime newStart = timestart + CDateTimeSpan(m_tmp_day-11-m_tmp_diff, 0, 0, 0);
288 CDateTime newEnd = timestop + CDateTimeSpan(m_tmp_day-11-m_tmp_diff, 0, 0, 0);
289 tag->SetStartFromLocalTime(newStart);
290 tag->SetEndFromLocalTime(newEnd);
292 EnableSettings(CONTROL_TMR_FIRST_DAY, false);
294 tag->m_bIsRepeating = false;
295 tag->m_iWeekdays = 0;
297 else if (setting.id == CONTROL_TMR_DAY && m_tmp_day <= 10)
299 EnableSettings(CONTROL_TMR_FIRST_DAY, true);
300 SetTimerFromWeekdaySetting(*tag);
302 else if (setting.id == CONTROL_TMR_BEGIN)
304 if (CGUIDialogNumeric::ShowAndGetTime(timerStartTime, g_localizeStrings.Get(14066)))
306 CDateTime timestart = timerStartTime;
307 int start_day = tag->StartAsLocalTime().GetDay();
308 int start_month = tag->StartAsLocalTime().GetMonth();
309 int start_year = tag->StartAsLocalTime().GetYear();
310 int start_hour = timestart.GetHour();
311 int start_minute = timestart.GetMinute();
312 CDateTime newStart(start_year, start_month, start_day, start_hour, start_minute, 0);
313 tag->SetStartFromLocalTime(newStart);
315 timerStartTimeStr = tag->StartAsLocalTime().GetAsLocalizedTime("", false);
316 UpdateSetting(CONTROL_TMR_BEGIN);
319 else if (setting.id == CONTROL_TMR_END)
321 if (CGUIDialogNumeric::ShowAndGetTime(timerEndTime, g_localizeStrings.Get(14066)))
323 CDateTime timestop = timerEndTime;
324 int start_day = tag->EndAsLocalTime().GetDay();
325 int start_month = tag->EndAsLocalTime().GetMonth();
326 int start_year = tag->EndAsLocalTime().GetYear();
327 int start_hour = timestop.GetHour();
328 int start_minute = timestop.GetMinute();
329 CDateTime newEnd(start_year, start_month, start_day, start_hour, start_minute, 0);
330 tag->SetEndFromLocalTime(newEnd);
332 timerEndTimeStr = tag->EndAsLocalTime().GetAsLocalizedTime("", false);
333 UpdateSetting(CONTROL_TMR_END);
336 else if (setting.id == CONTROL_TMR_FIRST_DAY && m_tmp_day <= 10)
338 CDateTime newFirstDay;
339 if (m_tmp_iFirstDay > 0)
340 newFirstDay = CDateTime::GetCurrentDateTime() + CDateTimeSpan(m_tmp_iFirstDay-1, 0, 0, 0);
342 tag->SetFirstDayFromLocalTime(newFirstDay);
345 tag->UpdateSummary();
348 void CGUIDialogPVRTimerSettings::SetTimer(CFileItem *item)
353 m_timerItem->GetPVRTimerInfoTag()->StartAsLocalTime().GetAsSystemTime(timerStartTime);
354 m_timerItem->GetPVRTimerInfoTag()->EndAsLocalTime().GetAsSystemTime(timerEndTime);
355 timerStartTimeStr = m_timerItem->GetPVRTimerInfoTag()->StartAsLocalTime().GetAsLocalizedTime("", false);
356 timerEndTimeStr = m_timerItem->GetPVRTimerInfoTag()->EndAsLocalTime().GetAsLocalizedTime("", false);
362 void CGUIDialogPVRTimerSettings::OnOkay()
365 CPVRTimerInfoTag* tag = m_timerItem->GetPVRTimerInfoTag();
367 // Set the timer's title to the channel name if it's 'New Timer' or empty
368 if (tag->m_strTitle == g_localizeStrings.Get(19056) || tag->m_strTitle.IsEmpty())
370 CPVRChannelPtr channel = g_PVRChannelGroups->GetByUniqueID(tag->m_iClientChannelUid, tag->m_iClientId);
372 tag->m_strTitle = channel->ChannelName();
376 tag->m_state = PVR_TIMER_STATE_SCHEDULED;
378 tag->m_state = PVR_TIMER_STATE_CANCELLED;