]> git.vomp.tv Git - vompclient.git/blob - vepgsettimer.cc
Rework master loop
[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 "remote.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 Remote::DF_LEFT:
214     case Remote::LEFT:
215     {
216       swap();
217       draw();
218       boxstack->update(this);
219       return 2;
220     }
221     case Remote::DF_RIGHT:
222     case Remote::RIGHT:
223     {
224       swap();
225       draw();
226       boxstack->update(this);
227       return 2;
228     }
229     case Remote::BACK:
230     {
231       return 4;
232     }
233     case Remote::OK:
234     {
235       if (selectedOption != YES) return 4;
236       doit();
237       return 4;
238     }
239   }
240
241   return 1;
242 }
243
244
245 void VEpgSetTimer::doit()
246 {
247   char* timerString = genTimerString();
248   logger->log("VEPGST", Log::DEBUG, "%s", timerString);
249
250   ULONG ret = vdr->setEventTimer(timerString);
251   delete[] timerString;
252
253   if (!vdr->isConnected())
254   {
255     Command::getInstance()->connectionLost();
256   }
257
258   if (ret == 0) logger->log("VEPGST", Log::DEBUG, "Success");
259   else if (ret == 1) logger->log("VEPGST", Log::DEBUG, "Fail: Timer already set for this event");
260   else if (ret == 2) logger->log("VEPGST", Log::DEBUG, "Fail: General failure setting timer");
261
262   VInfo* vi = new VInfo();
263   vi->setSize(400, 150);
264   vi->createBuffer();
265   vi->setExitable();
266   vi->setBorderOn(1);
267   vi->setTitleBarOn(0);
268
269   if (Video::getInstance()->getFormat() == Video::PAL)
270     vi->setPosition(170, 200);
271   else
272     vi->setPosition(160, 150);
273
274   if (ret == 0) vi->setOneLiner(tr("Timer set successfully"));
275   else if (ret == 1) vi->setOneLiner(tr("There is already a timer for this event"));
276   else if (ret == 2) vi->setOneLiner(tr("Failure setting timer"));
277   vi->draw();
278
279   Message* m = new Message();
280   m->message = Message::ADD_VIEW;
281   m->to = boxstack;
282   m->parameter = (ULONG)vi;
283   MessageQueue::getInstance()->postMessage(m);
284 }
285
286 void VEpgSetTimer::processMessage(Message* m)
287 {
288   if (m->message == Message::MOUSE_MOVE)
289   {
290     if (buttonYes.mouseMove((m->parameter>>16)-getScreenX(),(m->parameter&0xFFFF)-getScreenY()))
291     {
292       buttonNo.setActive(0);
293       selectedOption = YES;
294       draw();
295       boxstack->update(this);
296     }
297     else if (buttonNo.mouseMove((m->parameter>>16)-getScreenX(),(m->parameter&0xFFFF)-getScreenY()))
298     {
299       buttonYes.setActive(0);
300       selectedOption = NO;
301       draw();
302       boxstack->update(this);
303     }
304   }
305   else if (m->message == Message::MOUSE_LBDOWN)
306   {
307     if (buttonYes.mouseLBDOWN((m->parameter>>16)-getScreenX(),(m->parameter&0xFFFF)-getScreenY()))
308     {
309       boxstack->handleCommand(Remote::OK); //simulate OK press
310     }
311     else if (buttonNo.mouseLBDOWN((m->parameter>>16)-getScreenX(),(m->parameter&0xFFFF)-getScreenY()))
312     {
313       boxstack->handleCommand(Remote::OK); //simulate OK press
314     }
315     else
316     {
317       //check if press is outside this view! then simulate cancel
318       int x=(m->parameter>>16)-getScreenX();
319       int y=(m->parameter&0xFFFF)-getScreenY();
320       if (x<0 || y <0 || x>(int)getWidth() || y>(int)getHeight())
321       {
322         boxstack->handleCommand(Remote::BACK); //simulate cancel press
323       }
324     }
325   }
326 }