datechecker.cpp
00001 /* 00002 This file is part of KOrganizer. 00003 00004 Copyright (c) 2002 Adriaan de Groot <groot@kde.org> 00005 Copyright (c) 2004 Cornelius Schumacher <schumacher@kde.org> 00006 00007 This program is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation; either version 2 of the License, or 00010 (at your option) any later version. 00011 00012 This program is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with this program; if not, write to the Free Software 00019 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00020 00021 As a special exception, permission is given to link this program 00022 with any edition of TQt, and distribute the resulting executable, 00023 without including the source code for TQt in the source distribution. 00024 */ 00025 00026 #include <tqtimer.h> 00027 00028 #include <kdebug.h> 00029 #include <tdelocale.h> 00030 #include <tdeglobal.h> 00031 00032 #include "datechecker.h" 00033 00034 DateChecker::DateChecker( TQObject *parent, const char *name ) 00035 : TQObject( parent, name ), mUpdateTimer( 0 ) 00036 { 00037 enableRollover( FollowMonth ); 00038 } 00039 00040 DateChecker::~DateChecker() 00041 { 00042 } 00043 00044 void DateChecker::enableRollover( RolloverType r ) 00045 { 00046 switch( r ) { 00047 case None: 00048 if ( mUpdateTimer ) { 00049 mUpdateTimer->stop(); 00050 delete mUpdateTimer; 00051 mUpdateTimer = 0; 00052 } 00053 break; 00054 case FollowDay: 00055 case FollowMonth: 00056 if ( !mUpdateTimer ) { 00057 mUpdateTimer = new TQTimer( this, "mUpdateTimer" ); 00058 connect( mUpdateTimer, TQT_SIGNAL( timeout() ), 00059 TQT_SLOT( possiblyPastMidnight() ) ); 00060 } 00061 mUpdateTimer->start( 0, true ); 00062 mLastDayChecked = TQDate::currentDate(); 00063 } 00064 mUpdateRollover = r; 00065 } 00066 00067 void DateChecker::passedMidnight() 00068 { 00069 TQDate today = TQDate::currentDate(); 00070 00071 if ( today.month() != mLastDayChecked.month() ) { 00072 if ( mUpdateRollover == FollowMonth ) { 00073 emit monthPassed( today ); 00074 } 00075 } 00076 emit dayPassed( today ); 00077 } 00078 00079 void DateChecker::possiblyPastMidnight() 00080 { 00081 if ( mLastDayChecked != TQDate::currentDate() ) { 00082 passedMidnight(); 00083 mLastDayChecked = TQDate::currentDate(); 00084 } 00085 // Set the timer to go off 1 second after midnight 00086 // or after 8 minutes, whichever comes first. 00087 if ( mUpdateTimer ) { 00088 TQTime now = TQTime::currentTime(); 00089 TQTime midnight = TQTime( 23, 59, 59 ); 00090 int msecsWait = TQMIN( 480000, now.msecsTo( midnight ) + 2000 ); 00091 00092 mUpdateTimer->stop(); 00093 mUpdateTimer->start( msecsWait, true ); 00094 } 00095 } 00096 00097 #include "datechecker.moc"