]> git.vomp.tv Git - vompclient.git/blob - vtimerlist.cc
WIP [broken]
[vompclient.git] / vtimerlist.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 "vtimerlist.h"
22
23 #include "message.h"
24 #include "input.h"
25 #include "wsymbol.h"
26 #include "colour.h"
27 #include "video.h"
28 #include "i18n.h"
29 #include "timers.h"
30 #include "vtimeredit.h"
31 #include "command.h"
32 #include "boxstack.h"
33 #include "vdr.h"
34 #include "vinfo.h"
35 #include "log.h"
36 #include "staticartwork.h"
37
38 VTimerList::VTimerList()
39 {
40   recTimerList = NULL;
41
42   clockRegion.x = 420;
43   clockRegion.y = 0;
44   clockRegion.w = 150;
45   clockRegion.h = 30;
46
47
48
49   flipflop = true;
50
51   setSize(570, 420);
52   createBuffer();
53
54   indicatorsRegion.x = 6;
55   indicatorsRegion.y = 44;
56   indicatorsRegion.w = 18;
57   indicatorsRegion.h = 15 * (getFontHeight() + 1);
58
59   if (Video::getInstance()->getFormat() == Video::PAL)
60   {
61     setPosition(80, 70);
62   }
63   else
64   {
65     setPosition(70, 35);
66   }
67
68   setTitleBarOn(1);
69   setTitleText(tr("Timers"));
70   setTitleBarColour(DrawStyle::TITLEBARBACKGROUND);
71   TVMediaInfo *info= new TVMediaInfo();
72   info->setStaticArtwork(sa_timers);
73   setTitleBarIcon(info);
74
75   sl.setPosition(30, 30 + 5);
76   sl.setSize(area.w - 40, area.h - 30 - 15 - 30);
77   add(&sl);
78 }
79
80 void VTimerList::preDelete()
81 {
82   Timers::getInstance()->cancelTimer(this, 1);
83 }
84
85 VTimerList::~VTimerList()
86 {
87   if (recTimerList)
88   {
89     for (UINT i = 0; i < recTimerList->size(); i++)
90     {
91       delete (*recTimerList)[i];
92     }
93
94     recTimerList->clear();
95     delete recTimerList;
96   }
97 }
98
99 void VTimerList::draw()
100 {
101   // Draw statics
102
103   TBBoxx::draw();
104
105   WSymbol w;
106   TEMPADD(&w);
107
108   w.nextSymbol = WSymbol::UP;
109   w.setPosition(20, 385);
110   w.draw();
111
112   w.nextSymbol = WSymbol::DOWN;
113   w.setPosition(50, 385);
114   w.draw();
115
116   w.nextSymbol = WSymbol::SKIPBACK;
117   w.setPosition(85, 385);
118   w.draw();
119
120   w.nextSymbol = WSymbol::SKIPFORWARD;
121   w.setPosition(115, 385);
122   w.draw();
123
124   drawTextRJ("[ok] = edit", 560, 385, DrawStyle::LIGHTTEXT);
125
126   drawClock();
127   drawShowing();
128   drawIndicators();
129 }
130
131 bool VTimerList::load()
132 {
133   recTimerList = VDR::getInstance()->getRecTimersList();
134
135   if (!recTimerList) return false;
136
137   char strA[300];
138   char strB[300];
139
140   struct tm btime;
141
142   // FIXME all drawing stuff in this class and sl.clear somewhere?!
143
144   sl.addColumn(0);
145   sl.addColumn(110);
146
147   RecTimer* recTimer;
148   int first = 1;
149
150   for (UINT i = 0; i < recTimerList->size(); i++)
151   {
152     recTimer = (*recTimerList)[i];
153     time_t rectime = recTimer->startTime;
154     LOCALTIME_R((time_t*)&rectime, &btime);
155     strftime(strA, 299, "%d/%m %H:%M ", &btime);
156     SNPRINTF(strB, 299, "%s\t%s", strA, recTimer->getName());
157     sl.addOption(strB, (ULONG)recTimer, first);
158     first = 0;
159   }
160
161   return true;
162 }
163
164 void VTimerList::drawClock()
165 {
166   // Blank the area first
167 #ifndef GRADIENT_DRAWING
168   rectangle(area.w - 150, 0, 150, 30, titleBarColour);
169 #endif
170
171   char timeString[20];
172   time_t t;
173   time(&t);
174   struct tm tms;
175   LOCALTIME_R(&t, &tms);
176   strftime(timeString, 19, "%d/%m %H:%M:%S", &tms);
177   drawTextRJ(timeString, 560, 5, DrawStyle::LIGHTTEXT);
178
179   Timers::getInstance()->setTimerT(this, 1, t + 1);
180 }
181
182 void VTimerList::drawShowing()
183 {
184   int topOption = sl.getTopOption() + 1;
185   if (sl.getNumOptions() == 0) topOption = 0;
186 #ifndef GRADIENT_DRAWING
187   rectangle(220, 385, 180, 25, DrawStyle::VIEWBACKGROUND);
188 #endif
189   char showing[200];
190   sprintf(showing, tr("%i to %i of %i"), topOption, sl.getBottomOption(), sl.getNumOptions());
191   drawText(showing, 220, 385, DrawStyle::LIGHTTEXT);
192 }
193
194 void VTimerList::quickUpdate() { //only quick for plattform that need it!
195 #ifdef GRADIENT_DRAWING
196       draw();
197 #else
198       sl.draw();
199       drawShowing();
200       drawIndicators();
201 #endif
202 }
203
204 void VTimerList::drawIndicators()
205 {
206   int top = sl.getTopOption();
207   int bottom = sl.getBottomOption();
208   int yinc = getFontHeight() + 1;
209   RecTimer* recTimer;
210 #ifndef GRADIENT_DRAWING
211   rectangle(6, 44, 18, 15*yinc, DrawStyle::VIEWBACKGROUND);
212 #endif
213   // The indexes recorded from the wselectlist into the index member of the RecTimer
214   // Is the same as the position in the vector of RecTimers
215   // Because they are in order, they don't change order and wselectlist starts from 0 up consecutively
216
217   int ypos = 44;
218   for (int current = top; current < bottom; current++)
219   {
220     recTimer = (*recTimerList)[current];
221
222     if (recTimer->recording) // Flashing red square
223     {
224       if (flipflop)
225       {
226         rectangle(6, ypos, 18, 16, DrawStyle::RED);
227         drawText("R", 8, ypos-3, DrawStyle::LIGHTTEXT);
228       }
229     }
230     else if (recTimer->pending)
231     {
232       rectangle(6, ypos, 18, 16, DrawStyle::RED);
233       drawText("X", 8, ypos-3, DrawStyle::BLACK);
234     }
235     else if (recTimer->active == 0)
236     {
237       rectangle(6, ypos, 18, 16, DrawStyle::SELECTHIGHLIGHT);
238       drawText("X", 8, ypos-3, DrawStyle::BLACK);
239     }
240     else
241     {
242 //      if (flipflop) rectangle(6, ypos, 18, 16, DrawStyle::GREEN);
243     }
244
245     ypos += yinc;
246   }
247 }
248
249 void VTimerList::timercall(int clientReference)
250 {
251         flipflop = !flipflop;
252 #ifndef GRADIENT_DRAWING
253   drawClock();
254   BoxStack::getInstance()->update(this, &clockRegion);
255
256   drawIndicators();
257   BoxStack::getInstance()->update(this, &indicatorsRegion);
258 #else
259   draw();
260   BoxStack::getInstance()->update(this);
261 #endif
262 }
263
264 int VTimerList::handleCommand(int command)
265 {
266   switch(command)
267   {
268     case Input::UP:
269     {
270       sl.up();
271       quickUpdate();
272       BoxStack::getInstance()->update(this);
273       return 2;
274     }
275     case Input::DOWN:
276     {
277       sl.down();
278       quickUpdate();
279       BoxStack::getInstance()->update(this);
280       return 2;
281     }
282     case Input::SKIPBACK:
283     {
284       sl.pageUp();
285       quickUpdate();
286       BoxStack::getInstance()->update(this);
287       return 2;
288     }
289     case Input::SKIPFORWARD:
290     {
291       sl.pageDown();
292       quickUpdate();
293       BoxStack::getInstance()->update(this);
294       return 2;
295     }
296     case Input::OK:
297     {
298       RecTimer* recTimer = NULL;
299       if (recTimerList) recTimer = (RecTimer*)sl.getCurrentOptionData();
300       if (recTimer == NULL) return 2;
301
302       VTimerEdit* v = new VTimerEdit(recTimer);
303       v->setParent(this);
304       v->draw();
305       BoxStack::getInstance()->add(v);
306       BoxStack::getInstance()->update(v);
307
308       return 2;
309     }
310     case Input::BACK:
311     {
312       return 4;
313     }
314   }
315   // stop command getting to any more views
316   return 1;
317 }
318
319 void VTimerList::processMessage(Message* m)
320 {
321   if (m->message == Message::MOUSE_MOVE)
322   {
323     if (sl.mouseMove((m->parameter>>16)-getScreenX(),(m->parameter&0xFFFF)-getScreenY()))
324     {
325         quickUpdate();
326       BoxStack::getInstance()->update(this);
327     }
328   }
329   else if (m->message == Message::MOUSE_LBDOWN)
330   {
331     if (sl.mouseLBDOWN((m->parameter>>16)-getScreenX(),(m->parameter&0xFFFF)-getScreenY()))
332     {
333       BoxStack::getInstance()->handleCommand(Input::OK); //simulate OK press
334     }
335     else
336     {
337       //check if press is outside this view! then simulate cancel
338       int x=(m->parameter>>16)-getScreenX();
339       int y=(m->parameter&0xFFFF)-getScreenY();
340       if (x<0 || y <0 || x>(int)getWidth() || y>(int)getHeight())
341       {
342         BoxStack::getInstance()->handleCommand(Input::BACK); //simulate cancel press
343       }
344     }
345   }
346   else if (m->message == Message::DELETE_SELECTED_TIMER)
347   {
348     RecTimer* recTimer = (RecTimer*)sl.getCurrentOptionData();
349     if (recTimer == NULL) return;
350     Log::getInstance()->log("VTimerList", Log::DEBUG, "Got timer to delete");
351
352   
353     ULONG retval = VDR::getInstance()->deleteTimer(recTimer);
354     if (!VDR::getInstance()->isConnected()) { Command::getInstance()->connectionLost(); return; }
355     Log::getInstance()->log("VTimerList", Log::DEBUG, "Got return fron delete timer: %lu", retval);
356     
357     if (retval != 10)
358     {
359       VInfo* errorBox = new VInfo();
360       errorBox->setSize(360, 200);
361       errorBox->createBuffer();
362       if (Video::getInstance()->getFormat() == Video::PAL)
363         errorBox->setPosition(190, 170);
364       else
365         errorBox->setPosition(180, 120);
366
367     
368       if (retval == 1) errorBox->setOneLiner(tr("Timers being edited at VDR, please try later"));
369       else if (retval == 3)  errorBox->setOneLiner(tr("Unable to delete timer - timer is running"));
370       else if (retval == 4)  errorBox->setOneLiner(tr("Error - timer not found at VDR"));
371       else                   errorBox->setOneLiner(tr("Unknown error"));
372
373       errorBox->setExitable();
374       errorBox->setBorderOn(1);
375       errorBox->setTitleBarColour(DrawStyle::DANGER);
376       errorBox->okButton();
377       errorBox->draw();
378       BoxStack::getInstance()->add(errorBox);
379       BoxStack::getInstance()->update(errorBox);
380     }
381     
382     int saveIndex = sl.getCurrentOption();
383     int saveTop = sl.getTopOption();
384      
385     if (recTimerList)
386     {
387       for (UINT i = 0; i < recTimerList->size(); i++)
388       {
389         delete (*recTimerList)[i];
390       }
391
392       recTimerList->clear();
393       delete recTimerList;
394     }
395
396     sl.clear();
397     load();
398
399     sl.hintSetCurrent(saveIndex);
400     sl.hintSetTop(saveTop);
401     draw();
402     BoxStack::getInstance()->update(this);
403   }
404 }
405