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

clock.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002 ** Copyright (C) 2000-2002 Trolltech AS.  All rights reserved.
00003 **
00004 ** This file is part of the Qtopia Environment.
00005 **
00006 ** This file may be distributed and/or modified under the terms of the
00007 ** GNU General Public License version 2 as published by the Free Software
00008 ** Foundation and appearing in the file LICENSE.GPL included in the
00009 ** packaging of this file.
00010 **
00011 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 **
00014 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00015 **
00016 ** Contact info@trolltech.com if any conditions of this licensing are
00017 ** not clear to you.
00018 **
00019 **********************************************************************/
00020 // changes added and Copyright (C) by L. J. Potter <ljp@llornkcor.com> 2002
00021 // changes added and Copyright (C) by Holger Freyther 2004,2005
00022 
00023 #include "clock.h"
00024 
00025 #include "analogclock.h"
00026 
00027 #include <qtabwidget.h>
00028 
00029 #include <opie2/ofiledialog.h>
00030 #include <opie2/oresource.h>
00031 
00032 #include <qpe/qpeapplication.h>
00033 #include <qpe/qcopenvelope_qws.h>
00034 #include <qpe/config.h>
00035 #include <qpe/timestring.h>
00036 #include <qpe/alarmserver.h>
00037 #include <qpe/sound.h>
00038 #include <qsound.h>
00039 #include <qtimer.h>
00040 
00041 
00042 
00043 #include <qlcdnumber.h>
00044 #include <qslider.h>
00045 #include <qlabel.h>
00046 #include <qtimer.h>
00047 #include <qpushbutton.h>
00048 #include <qtoolbutton.h>
00049 #include <qpainter.h>
00050 #include <qmessagebox.h>
00051 #include <qdatetime.h>
00052 #include <qspinbox.h>
00053 #include <qcombobox.h>
00054 #include <qcheckbox.h>
00055 #include <qgroupbox.h>
00056 #include <qlayout.h>
00057 #include <qhbox.h>
00058 #include <qlineedit.h>
00059 
00060 static const int sw_prec = 2;
00061 static const int magic_daily = 2292922;
00062 static const int magic_countdown = 2292923;
00063 static const int magic_snooze = 2292924;
00064 static const int magic_playmp = 2292925;
00065 static const char ALARM_CLOCK_CHANNEL [] = "QPE/Application/clock";
00066 static const char ALARM_CLOCK_MESSAGE [] = "alarm(QDateTime,int)";
00067 
00068 #include <math.h>
00069 #include <unistd.h>
00070 #include <sys/types.h>
00071 
00072 #include <pthread.h>
00073 
00074 
00075 static void toggleScreenSaver( bool on )
00076 {
00077     QCopEnvelope e( "QPE/System", "setScreenSaverMode(int)" );
00078     e << ( on ? QPEApplication::Enable : QPEApplication::DisableSuspend );
00079 }
00080 
00081 static void startPlayer()
00082 {
00083     Config config( "qpe" );
00084     config.setGroup( "Time" );
00085     sleep(15);
00086     QCopEnvelope e( "QPE/Application/opieplayer", "setDocument(QString)" );
00087     e << config.readEntry( "mp3File", "" );
00088 }
00089 
00090 class MySpinBox : public QSpinBox
00091 {
00092 public:
00093     QLineEdit *lineEdit() const {
00094         return editor();
00095     }
00096 };
00097 
00098 //
00099 //
00100 //
00101 AlarmDlg::AlarmDlg(QWidget *parent, const char *name, bool modal,
00102     const QString &txt) :
00103     AlarmDlgBase(parent, name, modal)
00104 {
00105     setCaption( tr("Clock") );
00106     pixmap->setPixmap( Opie::Core::OResource::loadPixmap("clock/alarmbell") );
00107     alarmDlgLabel->setText(txt);
00108 
00109     connect(snoozeTime, SIGNAL(valueChanged(int)), this,
00110         SLOT(changePrompt(int)));
00111     connect(cmdOk, SIGNAL(clicked()), this, SLOT(checkSnooze()));
00112 }
00113 
00114 //
00115 //
00116 //
00117 void
00118 AlarmDlg::setText(const QString &txt)
00119 {
00120     alarmDlgLabel->setText(txt);
00121 }
00122 
00123 //
00124 //
00125 //
00126 void
00127 AlarmDlg::checkSnooze(void)
00128 {
00129     //
00130     // Ensure we have only one snooze alarm.
00131     //
00132     AlarmServer::deleteAlarm(QDateTime(), ALARM_CLOCK_CHANNEL,
00133         ALARM_CLOCK_MESSAGE, magic_snooze);
00134 
00135     if (snoozeTime->value() > 0) {
00136         QDateTime wake = QDateTime::currentDateTime();
00137         wake = wake.addSecs(snoozeTime->value() * 60); // snoozeTime in minutes
00138 
00139         AlarmServer::addAlarm(wake, ALARM_CLOCK_CHANNEL,
00140             ALARM_CLOCK_MESSAGE, magic_snooze);
00141     }
00142     accept();
00143 }
00144 
00145 
00146 
00147 void
00148 AlarmDlg::changePrompt(int mins)
00149 {
00150     cmdOk->setText(mins > 0 ? tr("Snooze") : tr("Close") );
00151 }
00152 
00153 
00154 
00155 Clock::Clock( QWidget * parent, const char *, WFlags f )
00156     : ClockBase( parent, "clock", f ), swatch_splitms(99), init(FALSE) // No tr
00157 {
00158     alarmDlg = 0;
00159     swLayout = 0;
00160     dayBtn = new QToolButton * [7];
00161 
00162     Config config( "qpe" );
00163     config.setGroup("Time");
00164     ampm = config.readBoolEntry( "AMPM", TRUE );
00165     onMonday = config.readBoolEntry( "MONDAY" );
00166 
00167     connect( tabs, SIGNAL(currentChanged(QWidget*)),
00168              this, SLOT(tabChanged(QWidget*)) );
00169 
00170     analogStopwatch = new AnalogClock( swFrame );
00171     stopwatchLcd = new QLCDNumber( swFrame );
00172     stopwatchLcd->setFrameStyle( QFrame::NoFrame );
00173     stopwatchLcd->setSegmentStyle( QLCDNumber::Flat );
00174     stopwatchLcd->setSizePolicy( QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred) );
00175 
00176     analogClock->display( QTime::currentTime() );
00177     clockLcd->setNumDigits( 5 );
00178     clockLcd->setFixedWidth( clockLcd->sizeHint().width() );
00179     date->setText( TimeString::dateString( QDate::currentDate(), TimeString::currentDateFormat() ) );
00180     if ( qApp->desktop()->width() < 200 )
00181         date->setFont( QFont(date->font().family(), 14, QFont::Bold) );
00182     if ( qApp->desktop()->height() > 240 ) {
00183         clockLcd->setFixedHeight( 30 );
00184         stopwatchLcd->setFixedHeight( 30 );
00185     }
00186 
00187     connect( stopStart, SIGNAL(pressed()), SLOT(stopStartStopWatch()) );
00188     connect( reset, SIGNAL(pressed()), SLOT(resetStopWatch()) );
00189 
00190     t = new QTimer( this );
00191     connect( t, SIGNAL(timeout()), SLOT(updateClock()) );
00192     t->start( 1000 );
00193 
00194     applyAlarmTimer = new QTimer( this );
00195     connect( applyAlarmTimer, SIGNAL(timeout()),
00196         this, SLOT(applyDailyAlarm()) );
00197 
00198     alarmt = new QTimer( this );
00199     connect( alarmt, SIGNAL(timeout()), SLOT(alarmTimeout()) );
00200 
00201     connect( qApp, SIGNAL(timeChanged()), SLOT(updateClock()) );
00202     connect( qApp, SIGNAL(timeChanged()), SLOT(applyDailyAlarm()) );
00203 
00204     swatch_running = FALSE;
00205     swatch_totalms = 0;
00206     swatch_currLap = 0;
00207     swatch_dispLap = 0;
00208     stopwatchLcd->setNumDigits( 8+1+sw_prec );
00209     stopwatchLcd->display( "00:00:00.00" );
00210 
00211     QVBoxLayout *lvb = new QVBoxLayout( lapFrame );
00212     nextLapBtn = new QToolButton( UpArrow, lapFrame );
00213     connect( nextLapBtn, SIGNAL(clicked()), this, SLOT(nextLap()) );
00214     nextLapBtn->setAccel( Key_Up );
00215     lvb->addWidget( nextLapBtn );
00216     prevLapBtn = new QToolButton( DownArrow, lapFrame );
00217     connect( prevLapBtn, SIGNAL(clicked()), this, SLOT(prevLap()) );
00218     prevLapBtn->setAccel( Key_Down );
00219     prevLapBtn->setMinimumWidth( 15 );
00220     lvb->addWidget( prevLapBtn );
00221     prevLapBtn->setEnabled( FALSE );
00222     nextLapBtn->setEnabled( FALSE );
00223 
00224     reset->setEnabled( FALSE );
00225 
00226     lapLcd->setNumDigits( 8+1+sw_prec );
00227     lapLcd->display( "00:00:00.00" );
00228 
00229     splitLcd->setNumDigits( 8+1+sw_prec );
00230     splitLcd->display( "00:00:00.00" );
00231 
00232     lapNumLcd->display( 1 );
00233 
00234     lapTimer = new QTimer( this );
00235     connect( lapTimer, SIGNAL(timeout()), this, SLOT(lapTimeout()) );
00236 
00237     for (uint s = 0; s < swatch_splitms.count(); s++ )
00238         swatch_splitms[(int)s] = 0;
00239 
00240     connect( qApp, SIGNAL(clockChanged(bool)), this, SLOT(changeClock(bool)) );
00241 
00242     cdGroup->hide(); // XXX implement countdown timer.
00243 
00244     connect( dailyHour, SIGNAL(valueChanged(int)), this, SLOT(scheduleApplyDailyAlarm()) );
00245     connect( dailyMinute, SIGNAL(valueChanged(int)), this, SLOT(setDailyMinute(int)) );
00246     connect( dailyAmPm, SIGNAL(activated(int)), this, SLOT(setDailyAmPm(int)) );
00247     connect( dailyEnabled, SIGNAL(toggled(bool)), this, SLOT(enableDaily(bool)) );
00248     cdLcd->display( "00:00" );
00249 
00250     dailyMinute->setValidator(0);
00251 
00252     Config cConfig( "Clock" ); // No tr
00253     cConfig.setGroup( "Daily Alarm" );
00254 
00255     QStringList days;
00256     days.append( tr("Mon", "Monday") );
00257     days.append( tr("Tue", "Tuesday") );
00258     days.append( tr("Wed", "Wednesday") );
00259     days.append( tr("Thu", "Thursday") );
00260     days.append( tr("Fri", "Friday") );
00261     days.append( tr("Sat", "Saturday") );
00262     days.append( tr("Sun", "Sunday") );
00263 
00264     int i;
00265     QHBoxLayout *hb = new QHBoxLayout( daysFrame );
00266     for ( i = 0; i < 7; i++ ) {
00267         dayBtn[i] = new QToolButton( daysFrame );
00268         hb->addWidget( dayBtn[i] );
00269         dayBtn[i]->setToggleButton( TRUE );
00270         dayBtn[i]->setOn( TRUE );
00271         dayBtn[i]->setFocusPolicy( StrongFocus );
00272         connect( dayBtn[i], SIGNAL(toggled(bool)), this, SLOT(scheduleApplyDailyAlarm()) );
00273     }
00274 
00275     for ( i = 0; i < 7; i++ )
00276         dayBtn[dayBtnIdx(i+1)]->setText( days[i] );
00277 
00278     QStringList exclDays = cConfig.readListEntry( "ExcludeDays", ',' );
00279     QStringList::Iterator it;
00280     for ( it = exclDays.begin(); it != exclDays.end(); ++it ) {
00281         int d = (*it).toInt();
00282         if ( d >= 1 && d <= 7 )
00283             dayBtn[dayBtnIdx(d)]->setOn( FALSE );
00284     }
00285 
00286     bool alarm = cConfig.readBoolEntry("Enabled", FALSE);
00287     bool sound = cConfig.readBoolEntry("SoundEnabled", FALSE );
00288     dailyEnabled->setChecked( alarm );
00289     sndGroup->setEnabled( alarm );
00290     sndCheck->setChecked( sound );
00291     sndChoose->setEnabled( sound );
00292     sndFileName->setEnabled( sound );
00293 
00294     // FIXME ODP migrate to own config class.. merge config options
00295     Config cfg_qpe( "qpe" );
00296     cfg_qpe.setGroup( "Time" );
00297     sndFileName->setText( cfg_qpe.readEntry( "mp3File" ) );
00298     //
00299 
00300     int m = cConfig.readNumEntry( "Minute", 0 );
00301     dailyMinute->setValue( m );
00302 //    dailyMinute->setPrefix( m <= 9 ? "0" : "" );
00303     int h = cConfig.readNumEntry( "Hour", 7 );
00304     if ( ampm ) {
00305         if (h > 12) {
00306             h -= 12;
00307             dailyAmPm->setCurrentItem( 1 );
00308         }
00309         if (h == 0) h = 12;
00310         dailyHour->setMinValue( 1 );
00311         dailyHour->setMaxValue( 12 );
00312     } else {
00313         dailyAmPm->hide();
00314     }
00315     dailyHour->setValue( h );
00316 
00317     connect( ((MySpinBox*)dailyHour)->lineEdit(), SIGNAL(textChanged(const QString&)),
00318             this, SLOT(dailyEdited()) );
00319     connect( ((MySpinBox*)dailyMinute)->lineEdit(), SIGNAL(textChanged(const QString&)),
00320             this, SLOT(dailyEdited()) );
00321 
00322 #if defined(Q_WS_QWS) && !defined(QT_NO_COP)
00323     connect( qApp, SIGNAL(appMessage(const QCString&,const QByteArray&)),
00324             this, SLOT(appMessage(const QCString&,const QByteArray&)) );
00325 #endif
00326 
00327     QTimer::singleShot( 0, this, SLOT(updateClock()) );
00328     swFrame->installEventFilter( this );
00329 
00330     init = TRUE;
00331 }
00332 
00333 Clock::~Clock()
00334 {
00335     toggleScreenSaver( true );
00336     delete [] dayBtn;
00337 }
00338 
00339 void Clock::updateClock()
00340 {
00341     if ( tabs->currentPageIndex() == 0 ) {
00342         QTime tm = QDateTime::currentDateTime().time();
00343         QString s;
00344         if ( ampm ) {
00345             int hour = tm.hour();
00346             if (hour == 0)
00347                 hour = 12;
00348             if (hour > 12)
00349                 hour -= 12;
00350             s.sprintf( "%2d%c%02d", hour, ':', tm.minute() );
00351             clockAmPm->setText( (tm.hour() >= 12) ? "PM" : "AM" );
00352             clockAmPm->show();
00353         } else {
00354             s.sprintf( "%2d%c%02d", tm.hour(), ':', tm.minute() );
00355             clockAmPm->hide();
00356         }
00357         clockLcd->display( s );
00358         clockLcd->repaint( FALSE );
00359         analogClock->display( QTime::currentTime() );
00360         date->setText( TimeString::dateString( QDate::currentDate(), TimeString::currentDateFormat() ) );
00361     } else if ( tabs->currentPageIndex() == 1 ) {
00362         int totalms = swatch_totalms;
00363         if ( swatch_running )
00364             totalms += swatch_start.elapsed();
00365         setSwatchLcd( stopwatchLcd, totalms, !swatch_running );
00366         QTime swatch_time = QTime(0,0,0).addMSecs(totalms);
00367         analogStopwatch->display( swatch_time );
00368         if ( swatch_dispLap == swatch_currLap ) {
00369             swatch_splitms[swatch_currLap] = swatch_totalms;
00370             if ( swatch_running )
00371                 swatch_splitms[swatch_currLap] += swatch_start.elapsed();
00372             updateLap();
00373         }
00374     } else if ( tabs->currentPageIndex() == 2 ) {
00375         // nothing.
00376     }
00377 }
00378 
00379 void Clock::changeClock( bool a )
00380 {
00381     if ( ampm != a ) {
00382         int minute = dailyMinute->value();
00383         int hour = dailyHour->value();
00384         if ( ampm ) {
00385             if (hour == 12)
00386                 hour = 0;
00387             if (dailyAmPm->currentItem() == 1 )
00388                 hour += 12;
00389             dailyHour->setMinValue( 0 );
00390             dailyHour->setMaxValue( 23 );
00391             dailyAmPm->hide();
00392         } else {
00393             if (hour > 12) {
00394                 hour -= 12;
00395                 dailyAmPm->setCurrentItem( 1 );
00396             }
00397             if (hour == 0) hour = 12;
00398             dailyHour->setMinValue( 1 );
00399             dailyHour->setMaxValue( 12 );
00400             dailyAmPm->show();
00401         }
00402         dailyMinute->setValue( minute );
00403         dailyHour->setValue( hour );
00404     }
00405     ampm = a;
00406     updateClock();
00407 }
00408 
00409 void Clock::stopStartStopWatch()
00410 {
00411     if ( swatch_running ) {
00412         swatch_totalms += swatch_start.elapsed();
00413         swatch_splitms[swatch_currLap] = swatch_totalms;
00414         stopStart->setText( tr("Start") );
00415         reset->setText( tr("Reset") );
00416         reset->setEnabled( TRUE );
00417         t->stop();
00418         swatch_running = FALSE;
00419         toggleScreenSaver( TRUE );
00420         updateClock();
00421     } else {
00422         swatch_start.start();
00423         stopStart->setText( tr("Stop") );
00424         reset->setText( tr("Lap/Split") );
00425         reset->setEnabled( swatch_currLap < 98 );
00426         t->start( 1000 );
00427         swatch_running = TRUE;
00428         // disable screensaver while stop watch is running
00429         toggleScreenSaver( FALSE );
00430     }
00431     swatch_dispLap = swatch_currLap;
00432     updateLap();
00433     prevLapBtn->setEnabled( swatch_dispLap );
00434     nextLapBtn->setEnabled( swatch_dispLap < swatch_currLap );
00435     stopStart->setAccel( Key_Return );
00436 }
00437 
00438 void Clock::resetStopWatch()
00439 {
00440     if ( swatch_running ) {
00441         swatch_splitms[swatch_currLap] = swatch_totalms+swatch_start.elapsed();
00442         swatch_dispLap = swatch_currLap;
00443         if ( swatch_currLap < 98 )  // allow up to 99 laps
00444             swatch_currLap++;
00445         reset->setEnabled( swatch_currLap < 98 );
00446         updateLap();
00447         lapTimer->start( 2000, TRUE );
00448     } else {
00449         swatch_start.start();
00450         swatch_totalms = 0;
00451         swatch_currLap = 0;
00452         swatch_dispLap = 0;
00453         for ( uint i = 0; i < swatch_splitms.count(); i++ )
00454             swatch_splitms[(int)i] = 0;
00455         updateLap();
00456         updateClock();
00457         reset->setText( tr("Lap/Split") );
00458         reset->setEnabled( FALSE );
00459     }
00460     prevLapBtn->setEnabled( swatch_dispLap );
00461     nextLapBtn->setEnabled( swatch_dispLap < swatch_currLap );
00462 }
00463 
00464 void Clock::prevLap()
00465 {
00466     if ( swatch_dispLap > 0 ) {
00467         swatch_dispLap--;
00468         updateLap();
00469         prevLapBtn->setEnabled( swatch_dispLap );
00470         nextLapBtn->setEnabled( swatch_dispLap < swatch_currLap );
00471     }
00472 }
00473 
00474 void Clock::nextLap()
00475 {
00476     if ( swatch_dispLap < swatch_currLap ) {
00477         swatch_dispLap++;
00478         updateLap();
00479         prevLapBtn->setEnabled( swatch_dispLap );
00480         nextLapBtn->setEnabled( swatch_dispLap < swatch_currLap );
00481     }
00482 }
00483 
00484 void Clock::lapTimeout()
00485 {
00486     swatch_dispLap = swatch_currLap;
00487     updateLap();
00488     prevLapBtn->setEnabled( swatch_dispLap );
00489     nextLapBtn->setEnabled( swatch_dispLap < swatch_currLap );
00490 }
00491 
00492 void Clock::updateLap()
00493 {
00494     if ( swatch_running && swatch_currLap == swatch_dispLap ) {
00495         swatch_splitms[swatch_currLap] = swatch_totalms;
00496         swatch_splitms[swatch_currLap] += swatch_start.elapsed();
00497     }
00498     int split = swatch_splitms[swatch_dispLap];
00499     int lap;
00500     if ( swatch_dispLap > 0 )
00501         lap = swatch_splitms[swatch_dispLap] - swatch_splitms[swatch_dispLap-1];
00502     else
00503         lap = swatch_splitms[swatch_dispLap];
00504 
00505     lapNumLcd->display( swatch_dispLap+1 );
00506     bool showMs = !swatch_running || swatch_dispLap!=swatch_currLap;
00507     setSwatchLcd( lapLcd, lap, showMs );
00508     setSwatchLcd( splitLcd, split, showMs );
00509 }
00510 
00511 void Clock::setSwatchLcd( QLCDNumber *lcd, int ms, bool showMs )
00512 {
00513     QTime swatch_time = QTime(0,0,0).addMSecs(ms);
00514     QString d = showMs ? QString::number(ms%1000+1000) : QString("    ");
00515     QString lcdtext = swatch_time.toString() + "." + d.right(3).left(sw_prec);
00516     lcd->display( lcdtext );
00517     lcd->repaint( FALSE );
00518 }
00519 
00520 bool Clock::eventFilter( QObject *o, QEvent *e )
00521 {
00522     if ( o == swFrame && e->type() == QEvent::Resize ) {
00523         QResizeEvent *re = (QResizeEvent *)e;
00524         delete swLayout;
00525         if ( re->size().height() < 80 || re->size().height()*3 < re->size().width() )
00526             swLayout = new QHBoxLayout( swFrame );
00527         else
00528             swLayout = new QVBoxLayout( swFrame );
00529         swLayout->addWidget( analogStopwatch );
00530         swLayout->addWidget( stopwatchLcd );
00531         swLayout->activate();
00532     }
00533 
00534     return FALSE;
00535 }
00536 
00537 void Clock::tabChanged( QWidget * )
00538 {
00539     if ( tabs->currentPageIndex() == 0 ) {
00540         t->start(1000);
00541     } else if ( tabs->currentPageIndex() == 1 ) {
00542         if ( !swatch_running )
00543             t->stop();
00544         stopStart->setAccel( Key_Return );
00545     } else if ( tabs->currentPageIndex() == 2 ) {
00546         t->start(1000);
00547     }
00548     updateClock();
00549 }
00550 
00551 void Clock::setDailyAmPm(int)
00552 {
00553     scheduleApplyDailyAlarm();
00554 }
00555 
00556 void Clock::setDailyMinute( int m )
00557 {
00558     dailyMinute->setPrefix( m <= 9 ? "0" : "" );
00559 }
00560 
00561 void Clock::dailyEdited()
00562 {
00563     if ( spinBoxValid(dailyMinute) && spinBoxValid(dailyHour) )
00564         scheduleApplyDailyAlarm();
00565     else
00566         applyAlarmTimer->stop();
00567 }
00568 
00569 void Clock::enableDaily( bool )
00570 {
00571     scheduleApplyDailyAlarm();
00572 }
00573 
00574 void Clock::appMessage( const QCString &msg, const QByteArray &data )
00575 {
00576     if ( msg == ALARM_CLOCK_MESSAGE ) {
00577         QDataStream ds(data,IO_ReadOnly);
00578         QDateTime when;
00579         int t;
00580         ds >> when >> t;
00581         QTime theTime( when.time() );
00582         if ( t == magic_daily || t == magic_snooze ||
00583              t == magic_playmp ) {
00584             QString msg = tr("<b>Daily Alarm:</b><p>");
00585             QString ts;
00586             if ( ampm ) {
00587             bool pm = FALSE;
00588                 int h = theTime.hour();
00589                 if (h > 12) {
00590                     h -= 12;
00591                     pm = TRUE;
00592                 }
00593                 if (h == 0) h = 12;
00594                 ts.sprintf( "%02d:%02d %s", h, theTime.minute(), pm?"PM":"AM" );
00595             } else {
00596                 ts.sprintf( "%02d:%02d", theTime.hour(), theTime.minute() );
00597             }
00598             msg += ts;
00599 
00600             if (t == magic_playmp ) {
00601                 pthread_t thread;
00602                 pthread_create(&thread,NULL, (void * (*) (void *))startPlayer, NULL/* &*/ );
00603             }else {
00604                 Sound::soundAlarm();
00605                 alarmCount = 0;
00606                 alarmt->start( 5000 );
00607             }
00608             if ( !alarmDlg ) {
00609                 alarmDlg = new AlarmDlg(this);
00610             }
00611             alarmDlg->setText(msg);
00612 
00613             // Set for tomorrow, so user wakes up every day, even if they
00614             // don't confirm the dialog.  Don't set it again when snoozing.
00615             if (t != magic_snooze) {
00616                 applyDailyAlarm();
00617             }
00618 
00619 
00620             if ( !alarmDlg->isVisible() ) {
00621                 QPEApplication::execDialog(alarmDlg);
00622                 alarmt->stop();
00623             }
00624         } else if ( t == magic_countdown ) {
00625             // countdown
00626             Sound::soundAlarm();
00627         }
00628     } else if ( msg == "setDailyEnabled(int)" ) {
00629         QDataStream ds(data,IO_ReadOnly);
00630         int enableDaily;
00631         ds >> enableDaily;
00632         dailyEnabled->setChecked( enableDaily );
00633         applyDailyAlarm();
00634     } else if ( msg == "editDailyAlarm()" ) {
00635         tabs->setCurrentPage(2);
00636         QPEApplication::setKeepRunning();
00637     } else if (msg == "showClock()") {
00638         tabs->setCurrentPage(0);
00639         QPEApplication::setKeepRunning();
00640     } else if (msg == "timerStart()" ) {
00641         if ( !swatch_running )
00642             stopStartStopWatch();
00643         tabs->setCurrentPage(1);
00644         QPEApplication::setKeepRunning();
00645     } else if (msg == "timerStop()" ) {
00646         if ( swatch_running )
00647             stopStartStopWatch();
00648         tabs->setCurrentPage(1);
00649         QPEApplication::setKeepRunning();
00650     } else if (msg == "timerReset()" ) {
00651         resetStopWatch();
00652         tabs->setCurrentPage(1);
00653         QPEApplication::setKeepRunning();
00654     }
00655 }
00656 
00657 void Clock::alarmTimeout()
00658 {
00659     if ( alarmCount < 10 ) {
00660         Sound::soundAlarm();
00661         alarmCount++;
00662     } else {
00663         alarmCount = 0;
00664         alarmt->stop();
00665     }
00666 }
00667 
00668 QDateTime Clock::nextAlarm( int h, int m )
00669 {
00670     QDateTime now = QDateTime::currentDateTime();
00671     QTime at( h, m );
00672     QDateTime when( now.date(), at );
00673     int count = 0;
00674     int dow = when.date().dayOfWeek();
00675     while ( when < now || !dayBtn[dayBtnIdx(dow)]->isOn() ) {
00676         when = when.addDays( 1 );
00677         dow = when.date().dayOfWeek();
00678         if ( ++count > 7 )
00679             return QDateTime();
00680     }
00681 
00682     return when;
00683 }
00684 
00685 int Clock::dayBtnIdx( int d ) const
00686 {
00687     if ( onMonday )
00688         return d-1;
00689     else if ( d == 7 )
00690         return 0;
00691     else
00692         return d;
00693 }
00694 
00695 void Clock::scheduleApplyDailyAlarm()
00696 {
00697     applyAlarmTimer->start( 5000, TRUE );
00698 }
00699 
00700 void Clock::applyDailyAlarm()
00701 {
00702     if ( !init )
00703         return;
00704 
00705     applyAlarmTimer->stop();
00706     int minute = dailyMinute->value();
00707     int hour = dailyHour->value();
00708     if ( ampm ) {
00709         if (hour == 12)
00710             hour = 0;
00711         if (dailyAmPm->currentItem() == 1 )
00712             hour += 12;
00713     }
00714 
00715     Config config( "Clock" );
00716     config.setGroup( "Daily Alarm" );
00717     config.writeEntry( "Hour", hour );
00718     config.writeEntry( "Minute", minute );
00719 
00720     bool enableDaily = dailyEnabled->isChecked();
00721     bool isSound     = sndCheck->isChecked();
00722     int  isMagic =  isSound ? magic_playmp : magic_daily;
00723     config.writeEntry( "Enabled", enableDaily );
00724     config.writeEntry( "SoundEnabled", isSound );
00725 
00726     QString exclDays;
00727     int exclCount = 0;
00728     for ( int i = 1; i <= 7; i++ ) {
00729         if ( !dayBtn[dayBtnIdx(i)]->isOn() ) {
00730             if ( !exclDays.isEmpty() )
00731                 exclDays += ",";
00732             exclDays += QString::number( i );
00733             exclCount++;
00734         }
00735     }
00736     config.writeEntry( "ExcludeDays", exclDays );
00737 
00738     /* try to delete all  */
00739     AlarmServer::deleteAlarm(QDateTime(), ALARM_CLOCK_CHANNEL,
00740                              ALARM_CLOCK_MESSAGE, magic_daily);
00741     AlarmServer::deleteAlarm(QDateTime(), ALARM_CLOCK_CHANNEL,
00742                              ALARM_CLOCK_MESSAGE, magic_playmp );
00743     AlarmServer::deleteAlarm(QDateTime(), ALARM_CLOCK_CHANNEL,
00744                              ALARM_CLOCK_MESSAGE, magic_snooze);
00745     
00746     if ( enableDaily && exclCount < 7 ) {
00747         QDateTime when = nextAlarm( hour, minute );
00748         AlarmServer::addAlarm(when, ALARM_CLOCK_CHANNEL,
00749                             ALARM_CLOCK_MESSAGE, isMagic);
00750     }
00751 }
00752 
00753 bool Clock::validDaysSelected(void)
00754 {
00755     for ( int i = 1; i <= 7; i++ ) {
00756         if ( dayBtn[dayBtnIdx(i)]->isOn() ) {
00757             return TRUE;
00758         }
00759     }
00760     return FALSE;
00761 }
00762 
00763 void Clock::closeEvent( QCloseEvent *e )
00764 {
00765     if (dailyEnabled->isChecked()) {
00766         if (!validDaysSelected()) {
00767             QMessageBox::warning(this, tr("Select Day"),
00768                 tr("Daily alarm requires at least\none day to be selected."));
00769             return;
00770         }
00771     }
00772 
00773     applyDailyAlarm();
00774     ClockBase::closeEvent(e);
00775 }
00776 
00777 bool Clock::spinBoxValid( QSpinBox *sb )
00778 {
00779     bool valid = TRUE;
00780     QString tv = sb->text();
00781     for ( uint i = 0; i < tv.length(); i++ ) {
00782         if ( !tv[0].isDigit() )
00783             valid = FALSE;
00784     }
00785     bool ok = FALSE;
00786     int v = tv.toInt( &ok );
00787     if ( !ok )
00788         valid = FALSE;
00789     if ( v < sb->minValue() || v > sb->maxValue() )
00790         valid = FALSE;
00791 
00792     return valid;
00793 }
00794 
00795 void Clock::slotBrowseMp3File() {
00796     Config config( "qpe" );
00797     config.setGroup("Time");
00798 
00799     QMap<QString, QStringList> map;
00800     map.insert(tr("All"), QStringList() );
00801     QStringList text;
00802     text << "audio/*";
00803     map.insert(tr("Audio"), text );
00804     QString str = Opie::Ui::OFileDialog::getOpenFileName( 2, QPEApplication::qpeDir() + "sounds/", QString::null, map);
00805     if(!str.isEmpty() ) {
00806         config.writeEntry("mp3Alarm",1);
00807         config.writeEntry("mp3File",str);
00808         sndFileName->setText( str );
00809         scheduleApplyDailyAlarm();
00810     }
00811 }

Generated on Sat Nov 5 16:15:24 2005 for OPIE by  doxygen 1.4.2