korganizer

datechecker.cpp
1 /*
2  This file is part of KOrganizer.
3 
4  Copyright (c) 2002 Adriaan de Groot <groot@kde.org>
5  Copyright (c) 2004 Cornelius Schumacher <schumacher@kde.org>
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 
21  As a special exception, permission is given to link this program
22  with any edition of TQt, and distribute the resulting executable,
23  without including the source code for TQt in the source distribution.
24 */
25 
26 #include <tqtimer.h>
27 
28 #include <kdebug.h>
29 #include <tdelocale.h>
30 #include <tdeglobal.h>
31 
32 #include "datechecker.h"
33 
34 DateChecker::DateChecker( TQObject *parent, const char *name )
35  : TQObject( parent, name ), mUpdateTimer( 0 )
36 {
37  enableRollover( FollowMonth );
38 }
39 
40 DateChecker::~DateChecker()
41 {
42 }
43 
44 void DateChecker::enableRollover( RolloverType r )
45 {
46  switch( r ) {
47  case None:
48  if ( mUpdateTimer ) {
49  mUpdateTimer->stop();
50  delete mUpdateTimer;
51  mUpdateTimer = 0;
52  }
53  break;
54  case FollowDay:
55  case FollowMonth:
56  if ( !mUpdateTimer ) {
57  mUpdateTimer = new TQTimer( this, "mUpdateTimer" );
58  connect( mUpdateTimer, TQ_SIGNAL( timeout() ),
59  TQ_SLOT( possiblyPastMidnight() ) );
60  }
61  mUpdateTimer->start( 0, true );
62  mLastDayChecked = TQDate::currentDate();
63  }
64  mUpdateRollover = r;
65 }
66 
67 void DateChecker::passedMidnight()
68 {
69  TQDate today = TQDate::currentDate();
70 
71  if ( today.month() != mLastDayChecked.month() ) {
72  if ( mUpdateRollover == FollowMonth ) {
73  emit monthPassed( today );
74  }
75  }
76  emit dayPassed( today );
77 }
78 
79 void DateChecker::possiblyPastMidnight()
80 {
81  if ( mLastDayChecked != TQDate::currentDate() ) {
82  passedMidnight();
83  mLastDayChecked = TQDate::currentDate();
84  }
85  // Set the timer to go off 1 second after midnight
86  // or after 8 minutes, whichever comes first.
87  if ( mUpdateTimer ) {
88  TQTime now = TQTime::currentTime();
89  TQTime midnight = TQTime( 23, 59, 59 );
90  int msecsWait = TQMIN( 480000, now.msecsTo( midnight ) + 2000 );
91 
92  mUpdateTimer->stop();
93  mUpdateTimer->start( msecsWait, true );
94  }
95 }
96 
97 #include "datechecker.moc"