korganizer

koalarmclient.cpp
1 /*
2  KOrganizer Alarm Daemon Client.
3 
4  This file is part of KOrganizer.
5 
6  Copyright (c) 2002,2003 Cornelius Schumacher
7 
8  This program is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 
22  As a special exception, permission is given to link this program
23  with any edition of TQt, and distribute the resulting executable,
24  without including the source code for TQt in the source distribution.
25 */
26 
27 #include "koalarmclient.h"
28 
29 #include "alarmdockwindow.h"
30 #include "alarmdialog.h"
31 
33 
34 #include <kstandarddirs.h>
35 #include <kdebug.h>
36 #include <tdelocale.h>
37 #include <tdeapplication.h>
38 #include <twin.h>
39 
40 #include <tqpushbutton.h>
41 
42 KOAlarmClient::KOAlarmClient( TQObject *parent, const char *name )
43  : DCOPObject( "ac" ), TQObject( parent, name ), mDialog( 0 )
44 {
45  kdDebug(5890) << "KOAlarmClient::KOAlarmClient()" << endl;
46 
47  mDocker = new AlarmDockWindow;
48  mDocker->show();
49  connect( this, TQ_SIGNAL( reminderCount( int ) ), mDocker, TQ_SLOT( slotUpdate( int ) ) );
50  connect( mDocker, TQ_SIGNAL( quitSignal() ), TQ_SLOT( slotQuit() ) );
51 
52  TDEConfig c( locate( "config", "korganizerrc" ) );
53  c.setGroup( "Time & Date" );
54  TQString tz = c.readEntry( "TimeZoneId" );
55  kdDebug(5890) << "TimeZone: " << tz << endl;
56 
57  mCalendar = new CalendarResources( tz );
58  mCalendar->readConfig();
59  mCalendar->load();
60 
61  connect( &mCheckTimer, TQ_SIGNAL( timeout() ), TQ_SLOT( checkAlarms() ) );
62 
63  TDEConfig *config = kapp->config();
64  config->setGroup( "Alarms" );
65  int interval = config->readNumEntry( "Interval", 60 );
66  kdDebug(5890) << "KOAlarmClient check interval: " << interval << " seconds."
67  << endl;
68  mLastChecked = config->readDateTimeEntry( "CalendarsLastChecked" );
69 
70  // load reminders that were active when quitting
71  config->setGroup( "General" );
72  int numReminders = config->readNumEntry( "Reminders", 0 );
73  for ( int i = 1; i <= numReminders; ++i ) {
74  TQString group( TQString( "Incidence-%1" ).arg( i ) );
75  config->setGroup( group );
76  TQString uid = config->readEntry( "UID" );
77  TQDateTime dt = config->readDateTimeEntry( "RemindAt" );
78  if ( !uid.isEmpty() ) {
79  Incidence *i = mCalendar->incidence( uid );
80  if ( i && !i->alarms().isEmpty() ) {
81  createReminder( mCalendar, i, dt, TQString() );
82  }
83  }
84  config->deleteGroup( group );
85  }
86  config->setGroup( "General" );
87  if (numReminders) {
88  config->writeEntry( "Reminders", 0 );
89  config->sync();
90  }
91 
92  checkAlarms();
93  mCheckTimer.start( 1000 * interval ); // interval in seconds
94 }
95 
96 KOAlarmClient::~KOAlarmClient()
97 {
98  delete mCalendar;
99  delete mDocker;
100  delete mDialog;
101 }
102 
103 void KOAlarmClient::checkAlarms()
104 {
105  TDEConfig *cfg = kapp->config();
106 
107  cfg->setGroup( "General" );
108  if ( !cfg->readBoolEntry( "Enabled", true ) ) return;
109 
110  TQDateTime from = mLastChecked.addSecs( 1 );
111  mLastChecked = TQDateTime::currentDateTime();
112 
113  kdDebug(5891) << "Check: " << from.toString() << " - " << mLastChecked.toString() << endl;
114 
115  TQValueList<Alarm *> alarms = mCalendar->alarms( from, mLastChecked );
116 
117  TQValueList<Alarm *>::ConstIterator it;
118  for( it = alarms.begin(); it != alarms.end(); ++it ) {
119  kdDebug(5891) << "REMINDER: " << (*it)->parent()->summary() << endl;
120  Incidence *incidence = mCalendar->incidence( (*it)->parent()->uid() );
121  createReminder( mCalendar, incidence, from, (*it)->text() );
122  }
123 }
124 
125 void KOAlarmClient::createReminder( KCal::CalendarResources *calendar,
126  KCal::Incidence *incidence,
127  const TQDateTime &dt,
128  const TQString &displayText )
129 {
130  if ( !incidence )
131  return;
132 
133  if ( !mDialog ) {
134  mDialog = new AlarmDialog( calendar );
135  connect( mDialog, TQ_SIGNAL(reminderCount(int)), mDocker, TQ_SLOT(slotUpdate(int)) );
136  connect( mDocker, TQ_SIGNAL(suspendAllSignal()), mDialog, TQ_SLOT(suspendAll()) );
137  connect( mDocker, TQ_SIGNAL(dismissAllSignal()), mDialog, TQ_SLOT(dismissAll()) );
138  connect( this, TQ_SIGNAL( saveAllSignal() ), mDialog, TQ_SLOT( slotSave() ) );
139  }
140 
141  mDialog->addIncidence( incidence, dt, displayText );
142  mDialog->wakeUp();
143  saveLastCheckTime();
144 }
145 
146 void KOAlarmClient::slotQuit()
147 {
148  emit saveAllSignal();
149  saveLastCheckTime();
150  quit();
151 }
152 
153 void KOAlarmClient::saveLastCheckTime()
154 {
155  TDEConfigGroup cg( TDEGlobal::config(), "Alarms");
156  cg.writeEntry( "CalendarsLastChecked", mLastChecked );
157  TDEGlobal::config()->sync();
158 }
159 
160 void KOAlarmClient::quit()
161 {
162  kdDebug(5890) << "KOAlarmClient::quit()" << endl;
163  kapp->quit();
164 }
165 
166 bool KOAlarmClient::commitData( TQSessionManager& )
167 {
168  emit saveAllSignal();
169  saveLastCheckTime();
170  return true;
171 }
172 
173 void KOAlarmClient::forceAlarmCheck()
174 {
175  checkAlarms();
176  saveLastCheckTime();
177 }
178 
179 void KOAlarmClient::dumpDebug()
180 {
181  TDEConfig *cfg = kapp->config();
182 
183  cfg->setGroup( "Alarms" );
184  TQDateTime lastChecked = cfg->readDateTimeEntry( "CalendarsLastChecked" );
185 
186  kdDebug(5890) << "Last Check: " << lastChecked << endl;
187 }
188 
189 TQStringList KOAlarmClient::dumpAlarms()
190 {
191  TQDateTime start = TQDateTime( TQDateTime::currentDateTime().date(),
192  TQTime( 0, 0 ) );
193  TQDateTime end = start.addDays( 1 ).addSecs( -1 );
194 
195  TQStringList lst;
196  // Don't translate, this is for debugging purposes.
197  lst << TQString("AlarmDeamon::dumpAlarms() from ") + start.toString()+ " to " +
198  end.toString();
199 
200  TQValueList<Alarm*> alarms = mCalendar->alarms( start, end );
201  TQValueList<Alarm*>::ConstIterator it;
202  for( it = alarms.begin(); it != alarms.end(); ++it ) {
203  Alarm *a = *it;
204  lst << TQString(" ") + a->parent()->summary() + " ("
205  + a->time().toString() + ")";
206  }
207 
208  return lst;
209 }
210 
211 void KOAlarmClient::debugShowDialog()
212 {
213 // showAlarmDialog();
214 }
215 
216 #include "koalarmclient.moc"
TQDateTime time() const
Incidence * parent() const
const Alarm::List & alarms() const
TQString summary() const