korganizer

koalarmclient.cpp

00001 /*
00002     KOrganizer Alarm Daemon Client.
00003 
00004     This file is part of KOrganizer.
00005 
00006     Copyright (c) 2002,2003 Cornelius Schumacher
00007 
00008     This program is free software; you can redistribute it and/or modify
00009     it under the terms of the GNU General Public License as published by
00010     the Free Software Foundation; either version 2 of the License, or
00011     (at your option) any later version.
00012 
00013     This program is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016     GNU General Public License for more details.
00017 
00018     You should have received a copy of the GNU General Public License
00019     along with this program; if not, write to the Free Software
00020     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00021 
00022     As a special exception, permission is given to link this program
00023     with any edition of Qt, and distribute the resulting executable,
00024     without including the source code for Qt in the source distribution.
00025 */
00026 
00027 #include "koalarmclient.h"
00028 
00029 #include "alarmdockwindow.h"
00030 #include "alarmdialog.h"
00031 
00032 #include <libkcal/calendarresources.h>
00033 
00034 #include <kstandarddirs.h>
00035 #include <kdebug.h>
00036 #include <klocale.h>
00037 #include <kapplication.h>
00038 #include <kwin.h>
00039 
00040 #include <qpushbutton.h>
00041 
00042 KOAlarmClient::KOAlarmClient( QObject *parent, const char *name )
00043   : DCOPObject( "ac" ), QObject( parent, name ), mDialog( 0 )
00044 {
00045   kdDebug(5890) << "KOAlarmClient::KOAlarmClient()" << endl;
00046 
00047   mDocker = new AlarmDockWindow;
00048   mDocker->show();
00049   connect( this, SIGNAL( reminderCount( int ) ), mDocker, SLOT( slotUpdate( int ) ) );
00050   connect( mDocker, SIGNAL( quitSignal() ), SLOT( slotQuit() ) );
00051 
00052   KConfig c( locate( "config", "korganizerrc" ) );
00053   c.setGroup( "Time & Date" );
00054   QString tz = c.readEntry( "TimeZoneId" );
00055   kdDebug(5890) << "TimeZone: " << tz << endl;
00056 
00057   mCalendar = new CalendarResources( tz );
00058   mCalendar->readConfig();
00059   mCalendar->load();
00060 
00061   connect( &mCheckTimer, SIGNAL( timeout() ), SLOT( checkAlarms() ) );
00062 
00063   KConfig *config = kapp->config();
00064   config->setGroup( "Alarms" );
00065   int interval = config->readNumEntry( "Interval", 60 );
00066   kdDebug(5890) << "KOAlarmClient check interval: " << interval << " seconds."
00067                 << endl;
00068   mLastChecked = config->readDateTimeEntry( "CalendarsLastChecked" );
00069 
00070   // load reminders that were active when quitting
00071   config->setGroup( "General" );
00072   int numReminders = config->readNumEntry( "Reminders", 0 );
00073   for ( int i=1; i<=numReminders; ++i )
00074   {
00075     QString group( QString( "Incidence-%1" ).arg( i ) );
00076     config->setGroup( group );
00077     QString uid = config->readEntry( "UID" );
00078     QDateTime dt = config->readDateTimeEntry( "RemindAt" );
00079     if ( !uid.isEmpty() )
00080       createReminder( mCalendar, mCalendar->incidence( uid ), dt, QString() );
00081     config->deleteGroup( group );
00082   }
00083   config->setGroup( "General" );
00084   if (numReminders) {
00085      config->writeEntry( "Reminders", 0 );
00086      config->sync();
00087   }
00088 
00089   checkAlarms();
00090   mCheckTimer.start( 1000 * interval );  // interval in seconds
00091 }
00092 
00093 KOAlarmClient::~KOAlarmClient()
00094 {
00095   delete mCalendar;
00096   delete mDocker;
00097   delete mDialog;
00098 }
00099 
00100 void KOAlarmClient::checkAlarms()
00101 {
00102   KConfig *cfg = kapp->config();
00103 
00104   cfg->setGroup( "General" );
00105   if ( !cfg->readBoolEntry( "Enabled", true ) ) return;
00106 
00107   QDateTime from = mLastChecked.addSecs( 1 );
00108   mLastChecked = QDateTime::currentDateTime();
00109 
00110   kdDebug(5891) << "Check: " << from.toString() << " - " << mLastChecked.toString() << endl;
00111 
00112   QValueList<Alarm *> alarms = mCalendar->alarms( from, mLastChecked );
00113 
00114   QValueList<Alarm *>::ConstIterator it;
00115   for( it = alarms.begin(); it != alarms.end(); ++it ) {
00116     kdDebug(5891) << "REMINDER: " << (*it)->parent()->summary() << endl;
00117     Incidence *incidence = mCalendar->incidence( (*it)->parent()->uid() );
00118     createReminder( mCalendar, incidence, from, (*it)->text() );
00119   }
00120 }
00121 
00122 void KOAlarmClient::createReminder( KCal::Calendar *calendar,
00123                                     KCal::Incidence *incidence,
00124                                     QDateTime dt,
00125                                     const QString &displayText )
00126 {
00127   if ( !incidence )
00128     return;
00129 
00130   if ( !mDialog ) {
00131     mDialog = new AlarmDialog( calendar );
00132     connect( mDialog, SIGNAL(reminderCount(int)), mDocker, SLOT(slotUpdate(int)) );
00133     connect( mDocker, SIGNAL(suspendAllSignal()), mDialog, SLOT(suspendAll()) );
00134     connect( mDocker, SIGNAL(dismissAllSignal()), mDialog, SLOT(dismissAll()) );
00135     connect( this, SIGNAL( saveAllSignal() ), mDialog, SLOT( slotSave() ) );
00136   }
00137 
00138   mDialog->addIncidence( incidence, dt, displayText );
00139   mDialog->wakeUp();
00140   saveLastCheckTime();
00141 }
00142 
00143 void KOAlarmClient::slotQuit()
00144 {
00145   emit saveAllSignal();
00146   saveLastCheckTime();
00147   quit();
00148 }
00149 
00150 void KOAlarmClient::saveLastCheckTime()
00151 {
00152   KConfigGroup cg( KGlobal::config(), "Alarms");
00153   cg.writeEntry( "CalendarsLastChecked", mLastChecked );
00154   KGlobal::config()->sync();
00155 }
00156 
00157 void KOAlarmClient::quit()
00158 {
00159   kdDebug(5890) << "KOAlarmClient::quit()" << endl;
00160   kapp->quit();
00161 }
00162 
00163 bool KOAlarmClient::commitData( QSessionManager& )
00164 {
00165   emit saveAllSignal();
00166   saveLastCheckTime();
00167   return true;
00168 }
00169 
00170 void KOAlarmClient::forceAlarmCheck()
00171 {
00172   checkAlarms();
00173   saveLastCheckTime();
00174 }
00175 
00176 void KOAlarmClient::dumpDebug()
00177 {
00178   KConfig *cfg = kapp->config();
00179 
00180   cfg->setGroup( "Alarms" );
00181   QDateTime lastChecked = cfg->readDateTimeEntry( "CalendarsLastChecked" );
00182 
00183   kdDebug(5890) << "Last Check: " << lastChecked << endl;
00184 }
00185 
00186 QStringList KOAlarmClient::dumpAlarms()
00187 {
00188   QDateTime start = QDateTime( QDateTime::currentDateTime().date(),
00189                                QTime( 0, 0 ) );
00190   QDateTime end = start.addDays( 1 ).addSecs( -1 );
00191 
00192   QStringList lst;
00193   // Don't translate, this is for debugging purposes.
00194   lst << QString("AlarmDeamon::dumpAlarms() from ") + start.toString()+ " to " +
00195          end.toString();
00196 
00197   QValueList<Alarm*> alarms = mCalendar->alarms( start, end );
00198   QValueList<Alarm*>::ConstIterator it;
00199   for( it = alarms.begin(); it != alarms.end(); ++it ) {
00200     Alarm *a = *it;
00201     lst << QString("  ") + a->parent()->summary() + " ("
00202               + a->time().toString() + ")";
00203   }
00204 
00205   return lst;
00206 }
00207 
00208 void KOAlarmClient::debugShowDialog()
00209 {
00210 //   showAlarmDialog();
00211 }
00212 
00213 #include "koalarmclient.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys