00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "predicttabwidget.h"
00032
00033 #include <qpe/config.h>
00034 #include <qpe/timeconversion.h>
00035
00036 #include <qlabel.h>
00037 #include <qlayout.h>
00038 #include <qpushbutton.h>
00039 #include <qscrollview.h>
00040 #include <qtable.h>
00041
00042 #include <stdlib.h>
00043
00044 PredictTabWidget::PredictTabWidget( QWidget *parent )
00045 : QWidget( parent, 0x0, 0 )
00046 {
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 QGridLayout *layout = new QGridLayout( this );
00058 layout->setMargin( 2 );
00059 layout->setSpacing( 4 );
00060
00061
00062 layout->addWidget( new QLabel( tr( "Predicted time drift" ), this ), 0, 0 );
00063 lblDrift = new QLabel( tr( "n/a" ), this );
00064 layout->addWidget( lblDrift, 0, 1 );
00065
00066
00067 layout->addWidget( new QLabel( tr( "Estimated shift" ), this ), 1, 0 );
00068 lblDiff = new QLabel( tr( "n/a" ), this );
00069 layout->addWidget( lblDiff, 1, 1 );
00070
00071
00072 layout->addWidget( new QLabel( tr( "Predicted time" ), this ), 2, 0 );
00073 lblPredicted = new QLabel( tr( "n/a" ), this );
00074 layout->addWidget( lblPredicted, 2, 1 );
00075
00076
00077 tblLookups = new QTable( 2, 3, this );
00078 QFont font( tblLookups->font() );
00079 font.setPointSize( font.pointSize() - 2 );
00080 tblLookups->setFont( font );
00081 tblLookups->horizontalHeader()->setLabel( 0, tr( "Shift [s/h]" ) );
00082 tblLookups->horizontalHeader()->setLabel( 1, tr( "Last [h]" ) );
00083 tblLookups->horizontalHeader()->setLabel( 2, tr( "Offset [s]" ) );
00084
00085 tblLookups->setMinimumHeight( 50 );
00086 tblLookups->setSizePolicy( QSizePolicy( QSizePolicy::Preferred, QSizePolicy::Maximum ) );
00087 layout->addMultiCellWidget( tblLookups, 3, 3, 0, 1 );
00088
00089
00090 QPushButton *pb = new QPushButton( tr( "Predict time" ), this );
00091 connect( pb, SIGNAL(clicked()), this, SLOT(slotPredictTime()) );
00092 layout->addWidget( pb, 4, 0 );
00093
00094
00095 pb = new QPushButton( tr( "Set predicted time" ), this );
00096 connect( pb, SIGNAL(clicked()), this, SLOT(slotSetPredictedTime()) );
00097 layout->addWidget( pb, 4, 1 );
00098
00099
00100 Config config( "ntp" );
00101 config.setGroup( "lookups" );
00102 int lookupCount = config.readNumEntry( "count", 0 );
00103 float last, shift, shiftPerSec;
00104 tblLookups->setNumRows( lookupCount );
00105 int cw = tblLookups->width() / 4;
00106 cw = 50;
00107 tblLookups->sortColumn( 0, FALSE, TRUE );
00108 _shiftPerSec = 0;
00109 QString grpname;
00110 for ( int i=0; i < lookupCount; i++ )
00111 {
00112 grpname = "lookup_";
00113 grpname.append( QString::number( i ) );
00114 config.setGroup( grpname );
00115 last = config.readEntry( "secsSinceLast", 0 ).toFloat();
00116 shift = QString( config.readEntry( "timeShift", 0 ) ).toFloat();
00117 shiftPerSec = shift / last;
00118 _shiftPerSec += shiftPerSec;
00119 tblLookups->setText( i, 0, QString::number( shiftPerSec * 60 * 60 ) );
00120 tblLookups->setText( i, 2, QString::number( shift ) );
00121 tblLookups->setText( i, 1, QString::number( last / ( 60 * 60 ) ) );
00122 }
00123 if(lookupCount)
00124 _shiftPerSec /= lookupCount;
00125 QString drift = QString::number( _shiftPerSec * 60 * 60);
00126 drift.append( tr( " s/h" ) );
00127 lblDrift->setText( drift );
00128
00129 Config lconfig( "locale" );
00130 lconfig.setGroup( "Location" );
00131 tz = lconfig.readEntry( "Timezone", "America/New_York" );
00132 }
00133
00134 PredictTabWidget::~PredictTabWidget()
00135 {
00136 }
00137
00138 void PredictTabWidget::setShiftPerSec( int i )
00139 {
00140 _shiftPerSec += i;
00141 }
00142
00143 void PredictTabWidget::slotTZChanged( const QString &newtz )
00144 {
00145 tz = newtz;
00146 }
00147
00148 void PredictTabWidget::slotPredictTime()
00149 {
00150 Config config( "ntp" );
00151 config.setGroup( "lookups" );
00152 int lastTime = config.readNumEntry( "time", 0 );
00153 config.writeEntry( "lastNtp", TRUE );
00154 setenv( "TZ", tz, 1 );
00155 int now = TimeConversion::toUTC( QDateTime::currentDateTime() );
00156 int corr = int( ( now - lastTime ) * _shiftPerSec );
00157 QString diff = QString::number( corr );
00158 diff.append( tr( " seconds" ) );
00159 lblDiff->setText( diff );
00160 predictedTime = QDateTime::currentDateTime().addSecs( corr );
00161 lblPredicted->setText( predictedTime.toString() );
00162 }
00163
00164 void PredictTabWidget::slotSetPredictedTime()
00165 {
00166 slotPredictTime();
00167 emit setTime( predictedTime );
00168 }