]> git.vomp.tv Git - vompclient.git/blob - vepgsettimer.cc
UDP button receiver port to Config
[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
81 VEpgSetTimer::~VEpgSetTimer()
82 {
83 }
84
85 char* VEpgSetTimer::genTimerString()
86 {
87   // Allocate to return
88   char* timerString = new char[1024];
89
90   // Other
91   struct tm btime;
92   int flags;
93   char dateString[20];
94   char startMargin[10];
95   time_t startTime;
96   char startString[10];
97   char endMargin[10];
98   time_t endTime;
99   char endString[10];
100   char priority[10];
101   char lifetime[10];
102   char* eventTitle;
103
104   flags = 1; // hard coded active timer flag
105
106   char* startMarginConfig = vdr->configLoad("Timers", "Start margin");      // NCONFIG
107   if (startMarginConfig)
108   {
109     strncpy(startMargin, startMarginConfig, 9);
110     delete[] startMarginConfig;
111   }
112   else strcpy(startMargin, "5");
113
114   startTime = event->time - (atoi(startMargin) * 60);
115   LOCALTIME_R(&startTime, &btime);
116   strftime(dateString, 19, "%Y-%m-%d", &btime);
117   strftime(startString, 9, "%H%M", &btime);
118
119   char* endMarginConfig = vdr->configLoad("Timers", "End margin");
120   if (endMarginConfig)
121   {
122     strncpy(endMargin, endMarginConfig, 9);
123     delete[] endMarginConfig;
124   }
125   else strcpy(endMargin, "5");
126
127   endTime = event->time + event->duration + (atoi(endMargin) * 60);
128   LOCALTIME_R(&endTime, &btime);
129   strftime(endString, 9, "%H%M", &btime);
130
131   char* priorityConfig = vdr->configLoad("Timers", "Priority");
132   if (priorityConfig)
133   {
134     strncpy(priority, priorityConfig, 9);
135     delete[] priorityConfig;
136   }
137   else strcpy(priority, "99");
138
139   char* lifetimeConfig = vdr->configLoad("Timers", "Lifetime");
140   if (lifetimeConfig)
141   {
142     strncpy(lifetime, lifetimeConfig, 9);
143     delete[] lifetimeConfig;
144   }
145   else strcpy(lifetime, "99");
146
147   eventTitle = new char[strlen(event->title.c_str()) + 1];
148   strcpy(eventTitle, event->title.c_str());
149   for(UINT i=0; i < strlen(eventTitle); i++) if (eventTitle[i] == ':') eventTitle[i] = '|';
150
151   SNPRINTF(timerString, 1023, "%i:%lu:%s:%s:%s:%s:%s:%s:",
152     flags, channel->number, dateString,
153     startString, endString,
154     priority, lifetime, eventTitle);
155
156   delete[] eventTitle;
157
158   return timerString;
159 }
160
161 void VEpgSetTimer::swap()
162 {
163   if (selectedOption == NO)
164   {
165     selectedOption = YES;
166     buttonYes.setActive(1);
167     buttonNo.setActive(0);
168   }
169   else if (selectedOption == YES)
170   {
171     selectedOption = NO;
172     buttonYes.setActive(0);
173     buttonNo.setActive(1);
174   }
175 }
176
177 void VEpgSetTimer::draw()
178 {
179   TBBoxx::draw();
180   drawPara(event->title.c_str(), 10, 40, DrawStyle::LIGHTTEXT);
181   drawText(channel->name, 10, 40 + (2 * getFontHeight()), DrawStyle::LIGHTTEXT);
182
183   char fullString[20];
184   time_t t;
185   struct tm btime;
186   char timeString[10];
187   time_t eventtime = event->time;
188   LOCALTIME_R(&eventtime, &btime);
189 #ifndef _MSC_VER
190   strftime(timeString, 9, "%0H:%0M - ", &btime); // and format it as hh:mm -
191 #else
192    strftime(timeString, 9, "%H:%M - ", &btime); // and format it as hh:mm -
193 #endif
194   strcpy(fullString, timeString); // put it in our buffer
195   t = event->time + event->duration; //get programme end time
196   LOCALTIME_R(&t, &btime);
197 #ifndef _MSC_VER
198   strftime(timeString, 9, "%0H:%0M", &btime); // and format it as hh:mm -
199 #else
200    strftime(timeString, 9, "%H:%M", &btime); // and format it as hh:mm -
201 #endif
202
203   strcat(fullString, timeString); // put it in our buffer
204
205   drawText(fullString, 10, 40 + (3 * getFontHeight()), DrawStyle::LIGHTTEXT);
206   drawText(tr("Create this timer?"), 10, 40 + (5 * getFontHeight()), DrawStyle::LIGHTTEXT);
207
208   buttonYes.draw();
209   buttonNo.draw();
210 }
211
212 int VEpgSetTimer::handleCommand(int command)
213 {
214   switch(command)
215   {
216     case Input::LEFT:
217     {
218       swap();
219       draw();
220       boxstack->update(this);
221       return 2;
222     }
223     case Input::RIGHT:
224     {
225       swap();
226       draw();
227       boxstack->update(this);
228       return 2;
229     }
230     case Input::BACK:
231     {
232       return 4;
233     }
234     case Input::OK:
235     {
236       if (selectedOption != YES) return 4;
237       doit();
238       return 4;
239     }
240   }
241
242   return 1;
243 }
244
245
246 void VEpgSetTimer::doit()
247 {
248   char* timerString = genTimerString();
249   logger->debug(TAG, "{}", timerString);
250
251   ULONG ret = vdr->setEventTimer(timerString);
252   delete[] timerString;
253
254   if (!vdr->isConnected())
255   {
256     Control::getInstance()->connectionLost();
257   }
258
259   if (ret == 0) logger->debug(TAG, "Success");
260   else if (ret == 1) logger->debug(TAG, "Fail: Timer already set for this event");
261   else if (ret == 2) logger->debug(TAG, "Fail: General failure setting timer");
262
263   VInfo* vi = new VInfo();
264   vi->setSize(400, 150);
265   vi->createBuffer();
266   vi->setExitable();
267   vi->setBorderOn(1);
268   vi->setTitleBarOn(0);
269
270   if (Video::getInstance()->getFormat() == Video::PAL)
271     vi->setPosition(170, 200);
272   else
273     vi->setPosition(160, 150);
274
275   if (ret == 0) vi->setOneLiner(tr("Timer set successfully"));
276   else if (ret == 1) vi->setOneLiner(tr("There is already a timer for this event"));
277   else if (ret == 2) vi->setOneLiner(tr("Failure setting timer"));
278   vi->draw();
279
280   Message* m = new Message();
281   m->message = Message::ADD_VIEW;
282   m->p_to = Message::BOXSTACK;
283   m->data = reinterpret_cast<void*>(vi);
284   MessageQueue::getInstance()->postMessage(m);
285 }
286
287 void VEpgSetTimer::processMessage(Message* m)
288 {
289   if (m->message == Message::MOUSE_MOVE)
290   {
291     if (buttonYes.mouseMove(m->parameter - getScreenX(), m->tag - getScreenY()))
292     {
293       buttonNo.setActive(0);
294       selectedOption = YES;
295       draw();
296       boxstack->update(this);
297     }
298     else if (buttonNo.mouseMove(m->parameter - getScreenX(), m->tag - getScreenY()))
299     {
300       buttonYes.setActive(0);
301       selectedOption = NO;
302       draw();
303       boxstack->update(this);
304     }
305   }
306   else if (m->message == Message::MOUSE_LBDOWN)
307   {
308     if (buttonYes.mouseLBDOWN(m->parameter - getScreenX(), m->tag - getScreenY()))
309     {
310       boxstack->handleCommand(Input::OK); //simulate OK press
311     }
312     else if (buttonNo.mouseLBDOWN(m->parameter - getScreenX(), m->tag - getScreenY()))
313     {
314       boxstack->handleCommand(Input::OK); //simulate OK press
315     }
316     else if (coordsOutsideBox(m))
317     {
318       boxstack->handleCommand(Input::BACK); //simulate cancel press
319     }
320   }
321 }