[cosmetics] update date in GPL header
[vuplus_xbmc] / xbmc / pvr / dialogs / GUIDialogPVRTimerSettings.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 "GUIDialogPVRTimerSettings.h"
22 #include "guilib/GUIKeyboardFactory.h"
23 #include "dialogs/GUIDialogNumeric.h"
24 #include "settings/GUISettings.h"
25 #include "guilib/LocalizeStrings.h"
26
27 #include "pvr/PVRManager.h"
28 #include "pvr/timers/PVRTimerInfoTag.h"
29 #include "pvr/channels/PVRChannelGroupsContainer.h"
30
31 using namespace std;
32 using namespace PVR;
33
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
46
47 CGUIDialogPVRTimerSettings::CGUIDialogPVRTimerSettings(void)
48   : CGUIDialogSettings(WINDOW_DIALOG_PVR_TIMER_SETTING, "DialogPVRTimerSettings.xml")
49 {
50   m_cancelled = true;
51   m_tmp_day   = 11;
52   m_loadType = LOAD_EVERY_TIME;
53 }
54
55 void CGUIDialogPVRTimerSettings::AddChannelNames(CFileItemList &channelsList, SETTINGSTRINGS &channelNames, bool bRadio)
56 {
57   g_PVRChannelGroups->GetGroupAll(bRadio)->GetMembers(channelsList);
58
59   channelNames.push_back("0 dummy");
60   for (int i = 0; i < channelsList.Size(); i++)
61   {
62     CStdString string;
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);
67   }
68
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);
72 }
73
74 void CGUIDialogPVRTimerSettings::SetWeekdaySettingFromTimer(const CPVRTimerInfoTag &timer)
75 {
76   if (timer.m_bIsRepeating)
77   {
78     if (timer.m_iWeekdays == 0x01)
79       m_tmp_day = 0;
80     else if (timer.m_iWeekdays == 0x02)
81       m_tmp_day = 1;
82     else if (timer.m_iWeekdays == 0x04)
83       m_tmp_day = 2;
84     else if (timer.m_iWeekdays == 0x08)
85       m_tmp_day = 3;
86     else if (timer.m_iWeekdays == 0x10)
87       m_tmp_day = 4;
88     else if (timer.m_iWeekdays == 0x20)
89       m_tmp_day = 5;
90     else if (timer.m_iWeekdays == 0x40)
91       m_tmp_day = 6;
92     else if (timer.m_iWeekdays == 0x1F)
93       m_tmp_day = 7;
94     else if (timer.m_iWeekdays == 0x3F)
95       m_tmp_day = 8;
96     else if (timer.m_iWeekdays == 0x7F)
97       m_tmp_day = 9;
98     else if (timer.m_iWeekdays == 0x60)
99       m_tmp_day = 10;
100   }
101 }
102
103 void CGUIDialogPVRTimerSettings::SetTimerFromWeekdaySetting(CPVRTimerInfoTag &timer)
104 {
105   timer.m_bIsRepeating = true;
106
107   if (m_tmp_day == 0)
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;
129   else
130     timer.m_iWeekdays = 0;
131 }
132
133 void CGUIDialogPVRTimerSettings::CreateSettings()
134 {
135   CPVRTimerInfoTag* tag = m_timerItem->GetPVRTimerInfoTag();
136
137   // clear out any old settings
138   m_settings.clear();
139
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);
144
145   if (tag->SupportsFolders())
146     AddButton(CONTROL_TMR_DIR, 19076, &tag->m_strDirectory, true);
147
148   AddBool(CONTROL_TMR_RADIO, 19077, &tag->m_bIsRadio);
149
150   /// Channel names
151   {
152     // For TV
153     CFileItemList channelslist_tv;
154     SETTINGSTRINGS channelstrings_tv;
155     AddChannelNames(channelslist_tv, channelstrings_tv, false);
156
157     // For Radio
158     CFileItemList channelslist_radio;
159     SETTINGSTRINGS channelstrings_radio;
160     AddChannelNames(channelslist_radio, channelstrings_radio, true);
161   }
162
163   /// Day
164   {
165     SETTINGSTRINGS daystrings;
166     tm time_cur;
167     tm time_tmr;
168
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();
173
174     /* get diffence of timer in days between today and timer start date */
175     time.GetAsTm(time_cur);
176     timestart.GetAsTm(time_tmr);
177
178     m_tmp_day += time_tmr.tm_yday - time_cur.tm_yday;
179     if (time_tmr.tm_yday - time_cur.tm_yday < 0)
180       m_tmp_day += 365;
181
182     for (int i = 1; i < 365; ++i)
183     {
184       CStdString string = time.GetAsLocalizedDate();
185       daystrings.push_back(string);
186       time += CDateTimeSpan(1, 0, 0, 0);
187     }
188
189     SetWeekdaySettingFromTimer(*tag);
190
191     AddSpin(CONTROL_TMR_DAY, 19079, &m_tmp_day, daystrings.size(), daystrings);
192   }
193
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);
198
199   /// First day
200   {
201     SETTINGSTRINGS daystrings;
202     tm time_cur;
203     tm time_tmr;
204
205     CDateTime time = CDateTime::GetCurrentDateTime();
206     CDateTime timestart = tag->FirstDayAsLocalTime();
207
208     /* get diffence of timer in days between today and timer start date */
209     if (time < timestart)
210     {
211       time.GetAsTm(time_cur);
212       timestart.GetAsTm(time_tmr);
213
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;
217     }
218
219     daystrings.push_back(g_localizeStrings.Get(19030));
220
221     for (int i = 1; i < 365; ++i)
222     {
223       CStdString string = time.GetAsLocalizedDate();
224       daystrings.push_back(string);
225       time += CDateTimeSpan(1, 0, 0, 0);
226     }
227
228     AddSpin(CONTROL_TMR_FIRST_DAY, 19084, &m_tmp_iFirstDay, daystrings.size(), daystrings);
229
230     if (tag->m_bIsRepeating)
231       EnableSettings(CONTROL_TMR_FIRST_DAY, true);
232     else
233       EnableSettings(CONTROL_TMR_FIRST_DAY, false);
234   }
235 }
236
237 void CGUIDialogPVRTimerSettings::OnSettingChanged(SettingInfo &setting)
238 {
239   CPVRTimerInfoTag* tag = m_timerItem->GetPVRTimerInfoTag();
240
241   if (setting.id == CONTROL_TMR_NAME)
242   {
243     if (CGUIKeyboardFactory::ShowAndGetInput(tag->m_strTitle, g_localizeStrings.Get(19097), false))
244     {
245       UpdateSetting(CONTROL_TMR_NAME);
246     }
247   }
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)
251   {
252     if (setting.id == CONTROL_TMR_RADIO)
253     {
254       EnableSettings(CONTROL_TMR_CHNAME_TV, !tag->m_bIsRadio);
255       EnableSettings(CONTROL_TMR_CHNAME_RADIO, tag->m_bIsRadio);
256     }
257
258     CFileItemPtr channel = g_PVRChannelGroups->GetGroupAll(tag->m_bIsRadio)->GetByChannelNumber(tag->m_iChannelNumber);
259     if (channel && channel->HasPVRChannelInfoTag())
260     {
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();
265
266       // Update channel pointer from above values
267       tag->UpdateChannel();
268     }
269   }
270   else if (setting.id == CONTROL_TMR_DAY && m_tmp_day > 10)
271   {
272     CDateTime time = CDateTime::GetCurrentDateTime();
273     CDateTime timestart = timerStartTime;
274     CDateTime timestop = timerEndTime;
275     int m_tmp_diff;
276     tm time_cur;
277     tm time_tmr;
278
279     /* get diffence of timer in days between today and timer start date */
280     time.GetAsTm(time_cur);
281     timestart.GetAsTm(time_tmr);
282
283     m_tmp_diff = time_tmr.tm_yday - time_cur.tm_yday;
284     if (time_tmr.tm_yday - time_cur.tm_yday < 0)
285       m_tmp_diff = 365;
286
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);
291
292     EnableSettings(CONTROL_TMR_FIRST_DAY, false);
293
294     tag->m_bIsRepeating = false;
295     tag->m_iWeekdays = 0;
296   }
297   else if (setting.id == CONTROL_TMR_DAY && m_tmp_day <= 10)
298   {
299     EnableSettings(CONTROL_TMR_FIRST_DAY, true);
300     SetTimerFromWeekdaySetting(*tag);
301   }
302   else if (setting.id == CONTROL_TMR_BEGIN)
303   {
304     if (CGUIDialogNumeric::ShowAndGetTime(timerStartTime, g_localizeStrings.Get(14066)))
305     {
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);
314
315       timerStartTimeStr = tag->StartAsLocalTime().GetAsLocalizedTime("", false);
316       UpdateSetting(CONTROL_TMR_BEGIN);
317     }
318   }
319   else if (setting.id == CONTROL_TMR_END)
320   {
321     if (CGUIDialogNumeric::ShowAndGetTime(timerEndTime, g_localizeStrings.Get(14066)))
322     {
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);
331
332       timerEndTimeStr = tag->EndAsLocalTime().GetAsLocalizedTime("", false);
333       UpdateSetting(CONTROL_TMR_END);
334     }
335   }
336   else if (setting.id == CONTROL_TMR_FIRST_DAY && m_tmp_day <= 10)
337   {
338     CDateTime newFirstDay;
339     if (m_tmp_iFirstDay > 0)
340       newFirstDay = CDateTime::GetCurrentDateTime() + CDateTimeSpan(m_tmp_iFirstDay-1, 0, 0, 0);
341
342     tag->SetFirstDayFromLocalTime(newFirstDay);
343   }
344
345   tag->UpdateSummary();
346 }
347
348 void CGUIDialogPVRTimerSettings::SetTimer(CFileItem *item)
349 {
350   m_timerItem         = item;
351   m_cancelled         = true;
352
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);
357
358   m_tmp_iFirstDay     = 0;
359   m_tmp_day           = 11;
360 }
361
362 void CGUIDialogPVRTimerSettings::OnOkay()
363 {
364   m_cancelled = false;
365   CPVRTimerInfoTag* tag = m_timerItem->GetPVRTimerInfoTag();
366
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())
369   {
370     CPVRChannelPtr channel = g_PVRChannelGroups->GetByUniqueID(tag->m_iClientChannelUid, tag->m_iClientId);
371     if (channel)
372       tag->m_strTitle = channel->ChannelName();
373   }
374
375   if (m_bTimerActive)
376     tag->m_state = PVR_TIMER_STATE_SCHEDULED;
377   else
378     tag->m_state = PVR_TIMER_STATE_CANCELLED;
379 }