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