trunk 20080912
[gitenigma.git] / src / time_correction.cpp
blob8c7d755e07d22f12d1615cb3ba54d4a1ddf1b34d
1 #include <src/time_correction.h>
2 #include <lib/dvb/edvb.h>
3 #include <lib/dvb/dvbservice.h>
5 eTimeCorrectionEditWindow::eTimeCorrectionEditWindow( tsref tp )
6 :updateTimer(eApp), transponder(tp)
8 init_eTimeCorrectionEditWindow(tp);
11 void eTimeCorrectionEditWindow::init_eTimeCorrectionEditWindow( tsref tp )
13 setText(_("Time Correction"));
14 move( ePoint(100,100) );
15 cresize( eSize(440,290 ) );
17 eLabel *l = new eLabel(this);
18 l->move( ePoint(10,10) );
19 l->resize( eSize(200,30));
20 l->setText(_("Receiver Time:"));
22 lCurTime = new eLabel(this);
23 lCurTime->move( ePoint(210,10) );
24 lCurTime->resize( eSize(90,30) );
26 lCurDate = new eLabel(this);
27 lCurDate->move( ePoint(320,10) );
28 lCurDate->resize( eSize(120,30) );
30 l = new eLabel(this);
31 l->move( ePoint(10,50) );
32 l->resize( eSize(200,30));
33 l->setText(_("Transponder Time:"));
35 lTpTime = new eLabel(this);
36 lTpTime->move( ePoint(210,50) );
37 lTpTime->resize( eSize(90,30) );
39 lTpDate = new eLabel(this);
40 lTpDate->move( ePoint(320,50) );
41 lTpDate->resize( eSize(120,30) );
43 l = new eLabel(this);
44 l->setText(_("New Time:"));
45 l->setFlags( eLabel::flagVCenter );
46 l->move( ePoint(10,90) );
47 l->resize( eSize(150,35) );
49 time_t now = time(0)+eDVB::getInstance()->time_difference;
50 tm tmp = *localtime( &now );
52 nTime = new eNumber(this, 2, 0, 59, 2, 0, 0, l);
53 nTime->resize(eSize(75,35));
54 nTime->move(ePoint(210,90));
55 nTime->setFlags( eNumber::flagTime|eNumber::flagFillWithZeros );
56 nTime->loadDeco();
57 nTime->setHelpText(_("enter correct time here"));
58 nTime->setNumber(0, tmp.tm_hour );
59 nTime->setNumber(1, tmp.tm_min );
60 CONNECT( nTime->selected, eTimeCorrectionEditWindow::fieldSelected );
62 l = new eLabel(this);
63 l->setText(_("New Date:"));
64 l->setFlags( eLabel::flagVCenter );
65 l->move( ePoint(10,140) );
66 l->resize( eSize(150,35) );
68 cday = new eComboBox(this);
69 cday->move(ePoint(210,140));
70 cday->resize(eSize(60,35));
71 cday->loadDeco();
72 cday->setHelpText(_("press ok to select another day"));
74 cmonth = new eComboBox( this );
75 cmonth->move(ePoint(280,140));
76 cmonth->resize(eSize(60,35));
77 cmonth->loadDeco();
78 cmonth->setHelpText(_("press ok to select another month"));
79 for ( int i = 0; i < 12; i++ )
80 new eListBoxEntryText( *cmonth, eString().sprintf("%02d",i+1), (void*)i );
81 CONNECT( cmonth->selchanged, eTimeCorrectionEditWindow::monthChanged );
83 cyear = new eComboBox(this);
84 cyear->move(ePoint(350,140));
85 cyear->resize(eSize(80,35));
86 cyear->loadDeco();
87 cyear->setHelpText(_("press ok to select another year"));
88 for ( int i = -1; i < 4; i++ )
89 new eListBoxEntryText( *cyear, eString().sprintf("%d",tmp.tm_year+(1900+i)), (void*)(tmp.tm_year+i) );
91 cyear->setCurrent( (void*) tmp.tm_year );
92 cmonth->setCurrent( (void*) tmp.tm_mon, true );
93 cday->setCurrent( (void*) tmp.tm_mday );
94 CONNECT( cyear->selchanged, eTimeCorrectionEditWindow::yearChanged );
96 bSet=new eButton(this);
97 bSet->setText(_("set"));
98 bSet->setShortcut("green");
99 bSet->setShortcutPixmap("green");
101 bSet->move(ePoint(10, clientrect.height()-100));
102 bSet->resize(eSize(220,40));
103 bSet->setHelpText(_("set new time and close window"));
104 bSet->loadDeco();
105 CONNECT(bSet->selected, eTimeCorrectionEditWindow::savePressed);
107 sbar = new eStatusBar(this);
108 sbar->move( ePoint(0, clientrect.height()-50) );
109 sbar->resize( eSize( clientrect.width(), 50) );
110 sbar->loadDeco();
111 CONNECT( updateTimer.timeout, eTimeCorrectionEditWindow::updateTimeDate );
114 void eTimeCorrectionEditWindow::savePressed()
116 eDVB &dvb = *eDVB::getInstance();
117 std::map<tsref,int> &tOffsMap=
118 eTransponderList::getInstance()->TimeOffsetMap;
119 time_t now = time(0)+dvb.time_difference;
121 tm nowTime = *localtime( &now );
122 nowTime.tm_isdst=-1;
123 nowTime.tm_hour = nTime->getNumber(0);
124 nowTime.tm_min = nTime->getNumber(1);
125 nowTime.tm_mday = (int)cday->getCurrent()->getKey();
126 nowTime.tm_wday = -1;
127 nowTime.tm_yday = -1;
128 nowTime.tm_mon = (int)cmonth->getCurrent()->getKey();
129 nowTime.tm_year = (int)cyear->getCurrent()->getKey();
130 time_t newTime = mktime(&nowTime);
132 tOffsMap.clear();
134 dvb.time_difference=1;
135 eDebug("[TIME] set Linux Time");
136 timeval tnow;
137 gettimeofday(&tnow, 0);
138 tnow.tv_sec=newTime;
139 settimeofday(&tnow, 0);
140 for (ePtrList<eMainloop>::iterator it(eMainloop::existing_loops)
141 ;it != eMainloop::existing_loops.end(); ++it)
142 it->setTimerOffset(newTime-now);
144 /*emit*/ dvb.timeUpdated();
146 // for calc new transponder correction
147 eDVBServiceController *sapi = dvb.getServiceAPI();
148 if ( sapi )
149 sapi->startTDT();
151 close(0);
154 void eTimeCorrectionEditWindow::updateTimeDate()
156 time_t now = time(0)+eDVB::getInstance()->time_difference;
157 tm ltime = *localtime( &now );
158 lCurTime->setText(eString().sprintf("%02d:%02d:%02d", ltime.tm_hour, ltime.tm_min, ltime.tm_sec));
159 lCurDate->setText(eString().sprintf("%02d.%02d.%04d", ltime.tm_mday, ltime.tm_mon+1, 1900+ltime.tm_year));
160 eDVBServiceController *sapi = eDVB::getInstance()->getServiceAPI();
161 if ( sapi && sapi->tdt && sapi->transponder )
163 time_t tpTime = now + sapi->lastTpTimeDifference;
164 tm ltime = *localtime( &tpTime );
165 lTpTime->setText(eString().sprintf("%02d:%02d:%02d", ltime.tm_hour, ltime.tm_min, ltime.tm_sec));
166 lTpDate->setText(eString().sprintf("%02d.%02d.%04d", ltime.tm_mday, ltime.tm_mon+1, 1900+ltime.tm_year));
168 else
170 lTpTime->setText(_("no transponder time"));
171 lTpDate->setText("");
175 int eTimeCorrectionEditWindow::eventHandler( const eWidgetEvent &event )
177 switch (event.type)
179 case eWidgetEvent::execBegin:
180 updateTimer.start(1000);
181 setFocus(bSet);
182 break;
183 case eWidgetEvent::execDone:
185 updateTimer.stop();
186 break;
188 default:
189 return eWindow::eventHandler( event );
191 return 1;
194 void eTimeCorrectionEditWindow::yearChanged( eListBoxEntryText* )
196 cmonth->setCurrent( (int) cmonth->getCurrent()->getKey(), true );
197 setFocus(cyear);
200 void eTimeCorrectionEditWindow::monthChanged( eListBoxEntryText *e )
202 if ( e )
204 const unsigned char monthdays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
205 int month = (int)e->getKey();
206 cday->clear();
207 int days = monthdays[month];
208 if ( month == 1 && __isleap( (int)cyear->getCurrent()->getKey()) )
209 days++;
210 for ( int i = 1; i <= days ; i++ )
211 new eListBoxEntryText( *cday, eString().sprintf("%02d", i), (void*)i);
212 cday->setCurrent( (void*) 1 );