]> git.vomp.tv Git - vompclient.git/blob - vepgsettimer.cc
Switch trunk code to new boxes
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 "video.h"
32 #include "remote.h"
33 #include "i18n.h"
34
35 VEpgSetTimer::VEpgSetTimer(Event* tevent, Channel* tchannel)
36 {
37   boxstack = BoxStack::getInstance();
38   vdr = VDR::getInstance();
39   logger = Log::getInstance();
40
41   event = tevent;
42   channel = tchannel;
43
44   setSize(400, 240);
45   createBuffer();
46   if (Video::getInstance()->getFormat() == Video::PAL)
47   {
48     setPosition(150, 170);
49   }
50   else
51   {
52     setPosition(140, 140);
53   }
54
55   setTitleBarOn(1);
56   setTitleBarColour(Colour::TITLEBARBACKGROUND);
57   setBorderOn(true);
58   setTitleText(tr("Set Timer"));
59
60   add(&buttonYes);
61   add(&buttonNo);
62
63   buttonYes.setPosition(80, 40 + (7 * surface->getFontHeight()));
64   buttonNo.setPosition(220, 40 + (7 * surface->getFontHeight()));
65
66   buttonYes.setText(tr("Yes"));
67   buttonNo.setText(tr("No"));
68   buttonYes.setActive(1);
69   selectedOption = YES;
70
71   logger->log("VEPGST", Log::DEBUG, "Title: %s", event->title);
72   logger->log("VEPGST", Log::DEBUG, "Time: %lu", event->time);
73   logger->log("VEPGST", Log::DEBUG, "Duration: %lu", event->duration);
74   logger->log("VEPGST", Log::DEBUG, "Channel: %i", channel->number);
75 }
76
77 VEpgSetTimer::~VEpgSetTimer()
78 {
79 }
80
81 char* VEpgSetTimer::genTimerString()
82 {
83   // Allocate to return
84   char* timerString = new char[1024];
85
86   // Other
87   struct tm* btime;
88   int flags;
89   char dateString[20];
90   char startMargin[10];
91   time_t startTime;
92   char startString[10];
93   char endMargin[10];
94   time_t endTime;
95   char endString[10];
96   char priority[10];
97   char lifetime[10];
98   char* eventTitle;
99
100   flags = 1; // hard coded active timer flag
101
102   btime = localtime((time_t*)&event->time);
103   strftime(dateString, 19, "%Y-%m-%d", btime);
104
105   char* startMarginConfig = vdr->configLoad("Timers", "Start margin");
106   if (startMarginConfig)
107   {
108     strncpy(startMargin, startMarginConfig, 9);
109     delete[] startMarginConfig;
110   }
111   else strcpy(startMargin, "5");
112
113   startTime = event->time - (atoi(startMargin) * 60);
114   btime = localtime(&startTime);
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   btime = localtime(&endTime);
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) + 1];
146   strcpy(eventTitle, event->title);
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, 10, 40, Colour::LIGHTTEXT);
179   drawText(channel->name, 10, 40 + (2 * surface->getFontHeight()), Colour::LIGHTTEXT);
180
181   char fullString[20];
182   time_t t;
183   struct tm* btime;
184   char timeString[10];
185   btime = localtime((time_t*)&event->time);
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   btime = localtime(&t);
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 * surface->getFontHeight()), Colour::LIGHTTEXT);
203   drawText(tr("Create this timer?"), 10, 40 + (5 * surface->getFontHeight()), Colour::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   Command::getInstance()->postMessageNoLock(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 }