]> git.vomp.tv Git - vompclient.git/blob - vepgsettimer.cc
43 CWFs
[vompclient.git] / vepgsettimer.cc
1 /*
2     Copyright 2004-2005 Chris Tallon
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP.  If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 #include <time.h>
21
22 #include "event.h"
23 #include "channel.h"
24 #include "boxstack.h"
25 #include "vdr.h"
26 #include "log.h"
27 #include "vinfo.h"
28 #include "message.h"
29 #include "control.h"
30 #include "messagequeue.h"
31 #include "video.h"
32 #include "input.h"
33 #include "i18n.h"
34
35 #include "vepgsettimer.h"
36
37 static const char* TAG = "VEPGST";
38
39 VEpgSetTimer::VEpgSetTimer(Event* tevent, Channel* tchannel)
40 {
41   boxstack = BoxStack::getInstance();
42   vdr = VDR::getInstance();
43   logger = LogNT::getInstance();
44
45   event = tevent;
46   channel = tchannel;
47
48   setSize(400, 240);
49   createBuffer();
50   if (Video::getInstance()->getFormat() == Video::PAL)
51   {
52     setPosition(150, 170);
53   }
54   else
55   {
56     setPosition(140, 140);
57   }
58
59   setTitleBarOn(1);
60   setTitleBarColour(DrawStyle::TITLEBARBACKGROUND);
61   setBorderOn(true);
62   setTitleText(tr("Set Timer"));
63
64   add(&buttonYes);
65   add(&buttonNo);
66
67   buttonYes.setPosition(80, 40 + (7 * getFontHeight()));
68   buttonNo.setPosition(220, 40 + (7 * getFontHeight()));
69
70   buttonYes.setText(tr("Yes"));
71   buttonNo.setText(tr("No"));
72   buttonYes.setActive(1);
73   selectedOption = YES;
74
75   logger->debug(TAG, "Title: {}", event->title);
76   logger->debug(TAG, "Time: {}", event->time);
77   logger->debug(TAG, "Duration: {}", event->duration);
78   logger->debug(TAG, "Channel: {}", channel->number);
79
80   MessageQueue::getInstance()->addReceiver(this);
81 }
82
83 VEpgSetTimer::~VEpgSetTimer()
84 {
85   MessageQueue::getInstance()->removeReceiver(this);
86 }
87
88 char* VEpgSetTimer::genTimerString()
89 {
90   // Allocate to return
91   char* timerString = new char[1024];
92
93   // Other
94   struct tm btime;
95   int flags;
96   char dateString[20];
97   char startMargin[10];
98   time_t startTime;
99   char startString[10];
100   char endMargin[10];
101   time_t endTime;
102   char endString[10];
103   char priority[10];
104   char lifetime[10];
105   char* eventTitle;
106
107   flags = 1; // hard coded active timer flag
108
109   char* startMarginConfig = vdr->configLoad("Timers", "Start margin");      // NCONFIG
110   if (startMarginConfig)
111   {
112     strncpy(startMargin, startMarginConfig, 9);
113     delete[] startMarginConfig;
114   }
115   else strcpy(startMargin, "5");
116
117   startTime = event->time - (atoi(startMargin) * 60);
118   LOCALTIME_R(&startTime, &btime);
119   strftime(dateString, 19, "%Y-%m-%d", &btime);
120   strftime(startString, 9, "%H%M", &btime);
121
122   char* endMarginConfig = vdr->configLoad("Timers", "End margin");
123   if (endMarginConfig)
124   {
125     strncpy(endMargin, endMarginConfig, 9);
126     delete[] endMarginConfig;
127   }
128   else strcpy(endMargin, "5");
129
130   endTime = event->time + event->duration + (atoi(endMargin) * 60);
131   LOCALTIME_R(&endTime, &btime);
132   strftime(endString, 9, "%H%M", &btime);
133
134   char* priorityConfig = vdr->configLoad("Timers", "Priority");
135   if (priorityConfig)
136   {
137     strncpy(priority, priorityConfig, 9);
138     delete[] priorityConfig;
139   }
140   else strcpy(priority, "99");
141
142   char* lifetimeConfig = vdr->configLoad("Timers", "Lifetime");
143   if (lifetimeConfig)
144   {
145     strncpy(lifetime, lifetimeConfig, 9);
146     delete[] lifetimeConfig;
147   }
148   else strcpy(lifetime, "99");
149
150   eventTitle = new char[strlen(event->title.c_str()) + 1];
151   strcpy(eventTitle, event->title.c_str());
152   for(UINT i=0; i < strlen(eventTitle); i++) if (eventTitle[i] == ':') eventTitle[i] = '|';
153
154   SNPRINTF(timerString, 1023, "%i:%lu:%s:%s:%s:%s:%s:%s:",
155     flags, channel->number, dateString,
156     startString, endString,
157     priority, lifetime, eventTitle);
158
159   delete[] eventTitle;
160
161   return timerString;
162 }
163
164 void VEpgSetTimer::swap()
165 {
166   if (selectedOption == NO)
167   {
168     selectedOption = YES;
169     buttonYes.setActive(1);
170     buttonNo.setActive(0);
171   }
172   else if (selectedOption == YES)
173   {
174     selectedOption = NO;
175     buttonYes.setActive(0);
176     buttonNo.setActive(1);
177   }
178 }
179
180 void VEpgSetTimer::draw()
181 {
182   TBBoxx::draw();
183   drawPara(event->title.c_str(), 10, 40, DrawStyle::LIGHTTEXT);
184   drawText(channel->name, 10, 40 + (2 * getFontHeight()), DrawStyle::LIGHTTEXT);
185
186   char fullString[20];
187   time_t t;
188   struct tm btime;
189   char timeString[10];
190   time_t eventtime = event->time;
191   LOCALTIME_R(&eventtime, &btime);
192 #ifndef _MSC_VER
193   strftime(timeString, 9, "%0H:%0M - ", &btime); // and format it as hh:mm -
194 #else
195    strftime(timeString, 9, "%H:%M - ", &btime); // and format it as hh:mm -
196 #endif
197   strcpy(fullString, timeString); // put it in our buffer
198   t = event->time + event->duration; //get programme end time
199   LOCALTIME_R(&t, &btime);
200 #ifndef _MSC_VER
201   strftime(timeString, 9, "%0H:%0M", &btime); // and format it as hh:mm -
202 #else
203    strftime(timeString, 9, "%H:%M", &btime); // and format it as hh:mm -
204 #endif
205
206   strcat(fullString, timeString); // put it in our buffer
207
208   drawText(fullString, 10, 40 + (3 * getFontHeight()), DrawStyle::LIGHTTEXT);
209   drawText(tr("Create this timer?"), 10, 40 + (5 * getFontHeight()), DrawStyle::LIGHTTEXT);
210
211   buttonYes.draw();
212   buttonNo.draw();
213 }
214
215 int VEpgSetTimer::handleCommand(int command)
216 {
217   switch(command)
218   {
219     case Input::LEFT:
220     {
221       swap();
222       draw();
223       boxstack->update(this);
224       return BoxStack::COMMAND_HANDLED;
225     }
226     case Input::RIGHT:
227     {
228       swap();
229       draw();
230       boxstack->update(this);
231       return BoxStack::COMMAND_HANDLED;
232     }
233     case Input::BACK:
234     {
235       return BoxStack::DELETE_ME;
236     }
237     case Input::OK:
238     {
239       if (selectedOption != YES) return 4;
240       doit();
241       return BoxStack::DELETE_ME;
242     }
243   }
244
245   return BoxStack::ABANDON_COMMAND;
246 }
247
248
249 void VEpgSetTimer::doit()
250 {
251   char* timerString = genTimerString();
252   logger->debug(TAG, "{}", timerString);
253
254   ULONG ret = vdr->setEventTimer(timerString);
255   delete[] timerString;
256
257   if (!vdr->isConnected())
258   {
259     Control::getInstance()->connectionLost();
260   }
261
262   if (ret == 0) logger->debug(TAG, "Success");
263   else if (ret == 1) logger->debug(TAG, "Fail: Timer already set for this event");
264   else if (ret == 2) logger->debug(TAG, "Fail: General failure setting timer");
265
266   VInfo* vi = new VInfo();
267   vi->setSize(400, 150);
268   vi->createBuffer();
269   vi->setExitable();
270   vi->setBorderOn(1);
271   vi->setTitleBarOn(0);
272
273   if (Video::getInstance()->getFormat() == Video::PAL)
274     vi->setPosition(170, 200);
275   else
276     vi->setPosition(160, 150);
277
278   if (ret == 0) vi->setOneLiner(tr("Timer set successfully"));
279   else if (ret == 1) vi->setOneLiner(tr("There is already a timer for this event"));
280   else if (ret == 2) vi->setOneLiner(tr("Failure setting timer"));
281   vi->draw();
282
283   Message* m = new Message();
284   m->message = Message::ADD_VIEW;
285   m->p_to = Message::BOXSTACK;
286   m->data = reinterpret_cast<void*>(vi);
287   MessageQueue::getInstance()->postMessage(m);
288 }
289
290 void VEpgSetTimer::processMessage(Message* m)
291 {
292   if (m->message == Message::MOUSE_MOVE)
293   {
294     if (buttonYes.mouseMove(m->parameter - getScreenX(), m->tag - getScreenY()))
295     {
296       buttonNo.setActive(0);
297       selectedOption = YES;
298       draw();
299       boxstack->update(this);
300     }
301     else if (buttonNo.mouseMove(m->parameter - getScreenX(), m->tag - getScreenY()))
302     {
303       buttonYes.setActive(0);
304       selectedOption = NO;
305       draw();
306       boxstack->update(this);
307     }
308   }
309   else if (m->message == Message::MOUSE_LBDOWN)
310   {
311     if (buttonYes.mouseLBDOWN(m->parameter - getScreenX(), m->tag - getScreenY()))
312     {
313       Input::sendInputKey(Input::OK);
314     }
315     else if (buttonNo.mouseLBDOWN(m->parameter - getScreenX(), m->tag - getScreenY()))
316     {
317       Input::sendInputKey(Input::OK);
318     }
319     else if (coordsOutsideBox(m))
320     {
321       Input::sendInputKey(Input::BACK);
322     }
323   }
324 }