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