Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

timetabwidget.cpp

Go to the documentation of this file.
00001 /*
00002                              This file is part of the Opie Project
00003 
00004                              Copyright (C) Opie Team <opie-devel@handhelds.org>
00005               =.
00006             .=l.
00007            .>+-=
00008  _;:,     .>    :=|.         This program is free software; you can
00009 .> <`_,   >  .   <=          redistribute it and/or  modify it under
00010 :`=1 )Y*s>-.--   :           the terms of the GNU Library General Public
00011 .="- .-=="i,     .._         License as published by the Free Software
00012  - .   .-<_>     .<>         Foundation; either version 2 of the License,
00013      ._= =}       :          or (at your option) any later version.
00014     .%`+i>       _;_.
00015     .i_,=:_.      -<s.       This program is distributed in the hope that
00016      +  .  -:.       =       it will be useful,  but WITHOUT ANY WARRANTY;
00017     : ..    .:,     . . .    without even the implied warranty of
00018     =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
00019   _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU
00020 ..}^=.=       =       ;      Library General Public License for more
00021 ++=   -.     .`     .:       details.
00022 :     =  ...= . :.=-
00023  -.   .:....=;==+<;          You should have received a copy of the GNU
00024   -_. . .   )=.  =           Library General Public License along with
00025     --        :-=`           this library; see the file COPYING.LIB.
00026                              If not, write to the Free Software Foundation,
00027                              Inc., 59 Temple Place - Suite 330,
00028                              Boston, MA 02111-1307, USA.
00029 */
00030 
00031 #include "timetabwidget.h"
00032 
00033 #include <opie2/oresource.h>
00034 
00035 #include <qpe/applnk.h>
00036 #include <qpe/config.h>
00037 #include <qpe/datebookmonth.h>
00038 #include <qpe/global.h>
00039 #include <qpe/tzselect.h>
00040 
00041 #if ( defined Q_WS_QWS || defined(_WS_QWS_) ) && !defined(QT_NO_COP)
00042 #include <qpe/qcopenvelope_qws.h>
00043 #endif
00044 
00045 #include <qcombobox.h>
00046 #include <qdatetime.h>
00047 #include <qframe.h>
00048 #include <qlabel.h>
00049 #include <qlayout.h>
00050 #include <qpushbutton.h>
00051 #include <qscrollview.h>
00052 #include <qspinbox.h>
00053 
00054 #include <stdlib.h>
00055 #include <sys/time.h>
00056 
00057 static const int ValueAM = 0;
00058 static const int ValuePM = 1;
00059 
00060 TimeTabWidget::TimeTabWidget( QWidget *parent )
00061         : QWidget( parent, 0x0, 0 )
00062 {
00063         // Synchronize HW clock to systemtime
00064         // This app will update systemtime
00065         //  - if Cancel is clicked, will reset systemtime to HW clock's time
00066         //  - if Ok is clicked, will leave systemtime as is
00067         system("/sbin/hwclock --systohc --utc");
00068 
00069         QVBoxLayout *tmpvb = new QVBoxLayout( this );
00070         QScrollView *sv = new QScrollView( this );
00071         tmpvb->addWidget( sv, 0, 0 );
00072         sv->setResizePolicy( QScrollView::AutoOneFit );
00073         sv->setFrameStyle( QFrame::NoFrame );
00074         QWidget *container = new QWidget( sv->viewport() );
00075         sv->addChild( container );
00076 
00077         QGridLayout *layout = new QGridLayout( container );
00078         layout->setMargin( 2 );
00079         layout->setSpacing( 4 );
00080 
00081         // Hours
00082         layout->addMultiCellWidget( new QLabel( tr( "Hour" ), container ), 1, 1, 0, 1 );
00083         sbHour = new QSpinBox( container );
00084         sbHour->setWrapping( TRUE );
00085         layout->addMultiCellWidget( sbHour, 2, 2, 0, 1 );
00086 
00087         // Minutes
00088         layout->addMultiCellWidget( new QLabel( tr( "Minute" ), container ), 1, 1, 2, 3 );
00089         sbMin = new QSpinBox( container );
00090         sbMin->setWrapping( TRUE );
00091         sbMin->setMinValue( 0 );
00092         sbMin->setMaxValue( 59 );
00093         layout->addMultiCellWidget( sbMin, 2, 2, 2, 3 );
00094 
00095         // AM/PM
00096         cbAmpm = new QComboBox( container );
00097         cbAmpm->insertItem( tr( "AM" ), ValueAM );
00098         cbAmpm->insertItem( tr( "PM" ), ValuePM );
00099         layout->addMultiCellWidget( cbAmpm, 2, 2, 4, 5 );
00100 
00101         // Date
00102         layout->addWidget( new QLabel( tr( "Date" ), container ), 4, 0 );
00103         btnDate = new DateButton( TRUE, container );
00104         layout->addMultiCellWidget( btnDate, 4, 4, 1, 5 );
00105 
00106         // Timezone
00107         layout->addMultiCellWidget( new QLabel( tr( "Time zone" ), container ), 6, 6, 0, 1 );
00108         selTimeZone = new TimeZoneSelector( container );
00109         connect( selTimeZone, SIGNAL(signalNewTz(const QString&)), this, SLOT(slotTZChanged(const QString&)) );
00110         layout->addMultiCellWidget( selTimeZone, 6, 6, 2, 5 );
00111 
00112         // Space filler
00113         layout->addItem( new QSpacerItem( 1, 1, QSizePolicy::Minimum, QSizePolicy::Expanding ), 7, 0 );
00114 
00115         // Set NTP time button
00116     m_ntpBtn = new QPushButton( Opie::Core::OResource::loadPixmap( "netsystemtime/ntptab", Opie::Core::OResource::SmallIcon ),
00117                                 tr( "Get time from the network" ), container );
00118     m_ntpBtn->setMinimumHeight( AppLnk::smallIconSize()+4 );
00119         connect( m_ntpBtn, SIGNAL(clicked()), this, SIGNAL(getNTPTime()) );
00120         layout->addMultiCellWidget( m_ntpBtn, 8, 8, 0, 5 );
00121 
00122         // Set predicted time button
00123         QPushButton *pb = new QPushButton( Opie::Core::OResource::loadPixmap( "netsystemtime/predicttab", Opie::Core::OResource::SmallIcon ),
00124                                        tr( "Set predicted time" ), container );
00125     pb->setMinimumHeight( AppLnk::smallIconSize()+4 );
00126         connect( pb, SIGNAL(clicked()), this, SIGNAL(getPredictedTime()) );
00127         layout->addMultiCellWidget( pb, 9, 9, 0, 5 );
00128 
00129         // Space filler at bottom of widget
00130         layout->addItem( new QSpacerItem( 1, 1, QSizePolicy::Minimum, QSizePolicy::Expanding ), 10, 0 );
00131 
00132         // Initialize values
00133         Config config( "locale" );
00134         config.setGroup( "Location" );
00135         selTimeZone->setCurrentZone( config.readEntry( "Timezone", "America/New_York" ) );
00136         use12HourTime = FALSE;
00137         setDateTime( QDateTime::currentDateTime() );
00138 }
00139 
00140 TimeTabWidget::~TimeTabWidget()
00141 {
00142 }
00143 
00144 void TimeTabWidget::saveSettings( bool commit )
00145 {
00146         if ( commit )
00147         {
00148                 // Set timezone and announce to world
00149                 QString tz = selTimeZone->currentZone();
00150                 Config config("locale");
00151                 config.setGroup( "Location" );
00152                 config.writeEntry( "Timezone", tz );
00153                 setenv( "TZ", tz, 1 );
00154                 QCopEnvelope setTimeZone( "QPE/System", "timeChange(QString)" );
00155                 setTimeZone << tz;
00156 
00157                 // If controls have a valid date & time, update systemtime
00158                 int hour = sbHour->value();
00159                 if ( use12HourTime && cbAmpm->currentItem() == ValuePM )
00160                         hour += 12;
00161                 QDateTime dt( btnDate->date(), QTime ( hour, sbMin->value(), QTime::currentTime().second() ) );
00162                 setSystemTime( dt );
00163         }
00164         else
00165         {
00166                 // Reset systemtime to hardware clock (i.e. undo any changes made by this app)
00167                 system("/sbin/hwclock --hctosys --utc");
00168         }
00169 }
00170 
00171 void TimeTabWidget::setDateTime( const QDateTime &dt )
00172 {
00173         // Set time
00174         QTime t = dt.time();
00175         if( use12HourTime )
00176         {
00177                 int show_hour = t.hour();
00178                 if ( t.hour() >= 12 )
00179                 {
00180                         show_hour -= 12;
00181                         cbAmpm->setCurrentItem( ValuePM );
00182                 }
00183                 else
00184                 {
00185                         cbAmpm->setCurrentItem( ValueAM );
00186                 }
00187                 if ( show_hour == 0 )
00188                         show_hour = 12;
00189                 sbHour->setValue( show_hour );
00190         }
00191         else
00192         {
00193                 sbHour->setValue( t.hour() );
00194         }
00195         sbMin->setValue( t.minute() );
00196 
00197         // Set date
00198         btnDate->setDate( dt.date() );
00199 }
00200 
00201 void TimeTabWidget::setNTPBtnEnabled( bool enabled )
00202 {
00203     m_ntpBtn->setEnabled( enabled );
00204 }
00205 
00206 void TimeTabWidget::setSystemTime( const QDateTime &dt )
00207 {
00208         // Set system clock
00209         if ( dt.isValid() )
00210         {
00211                 struct timeval myTv;
00212                 int t = TimeConversion::toUTC( dt );
00213                         myTv.tv_sec = t;
00214                 myTv.tv_usec = 0;
00215 
00216                 if ( myTv.tv_sec != -1 )
00217                     ::settimeofday( &myTv, 0 );
00218 
00219                 /*
00220                  * Commit the datetime to the  'hardware'
00221                  * as Global::writeHWClock() is a NOOP with Opie Alarm
00222                  */
00223                 system("/sbin/hwclock --systohc --utc");
00224         }
00225 }
00226 
00227 void TimeTabWidget::slotUse12HourTime( int i )
00228 {
00229         use12HourTime = (i == 1);
00230 
00231         cbAmpm->setEnabled( use12HourTime );
00232 
00233         int show_hour = sbHour->value();
00234 
00235         if ( use12HourTime )
00236         {
00237                 sbHour->setMinValue( 1 );
00238                 sbHour->setMaxValue( 12 );
00239 
00240                 if ( show_hour >= 12 )
00241                 {
00242                         show_hour -= 12;
00243                         cbAmpm->setCurrentItem( ValuePM );
00244                 }
00245                 else
00246                 {
00247                         cbAmpm->setCurrentItem( ValueAM );
00248                 }
00249                 if ( show_hour == 0 )
00250                         show_hour = 12;
00251         }
00252         else
00253         {
00254                 sbHour->setMinValue( 0 );
00255                 sbHour->setMaxValue( 23 );
00256 
00257                 if ( cbAmpm->currentItem() == ValuePM )
00258                 {
00259                         show_hour += 12;
00260                         if ( show_hour == 24 )
00261                                 show_hour = 0;
00262                 }
00263         }
00264 
00265         sbHour->setValue( show_hour );
00266 }
00267 
00268 void TimeTabWidget::slotDateFormatChanged( const DateFormat &df )
00269 {
00270         btnDate->setDateFormat( df );
00271 }
00272 
00273 void TimeTabWidget::slotWeekStartChanged( int monday )
00274 {
00275         btnDate->setWeekStartsMonday( monday );
00276 }
00277 
00278 void TimeTabWidget::slotTZChanged( const QString &newtz )
00279 {
00280         // If controls have a valid date & time, update systemtime
00281         int hour = sbHour->value();
00282         if ( use12HourTime && cbAmpm->currentItem() == ValuePM )
00283                 hour += 12;
00284         QDateTime dt( btnDate->date(), QTime ( hour, sbMin->value(), QTime::currentTime().second() ) );
00285         setSystemTime( dt );
00286         QCopEnvelope setTimeZone( "QPE/System", "timeChange(QString)" );
00287         setTimeZone << newtz;
00288 
00289         // Set system timezone
00290         QString currtz = getenv( "TZ" );
00291         setenv( "TZ", newtz, 1 );
00292 
00293         // Get new date/time
00294         hour = sbHour->value();
00295         if ( use12HourTime && cbAmpm->currentItem() == ValuePM )
00296                 hour += 12;
00297         dt = QDateTime::currentDateTime();
00298 
00299         // Reset system timezone
00300         if ( !currtz.isNull() )
00301         {
00302                 setenv( "TZ", currtz, 1 );
00303         }
00304 
00305         // Set controls to new time
00306         setDateTime( dt );
00307 
00308         emit tzChanged( newtz );
00309 }

Generated on Sat Nov 5 16:17:48 2005 for OPIE by  doxygen 1.4.2