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