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 TQt, and distribute the resulting executable,
00024     without including the source code for TQt 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 <tqpushbutton.h>
00041 
00042 KOAlarmClient::KOAlarmClient( TQObject *parent, const char *name )
00043   : DCOPObject( "ac" ), TQObject( parent, name ), mDialog( 0 )
00044 {
00045   kdDebug(5890) << "KOAlarmClient::KOAlarmClient()" << endl;
00046 
00047   mDocker = new AlarmDockWindow;
00048   mDocker->show();
00049   connect( this, TQT_SIGNAL( reminderCount( int ) ), mDocker, TQT_SLOT( slotUpdate( int ) ) );
00050   connect( mDocker, TQT_SIGNAL( quitSignal() ), TQT_SLOT( slotQuit() ) );
00051 
00052   KConfig c( locate( "config", "korganizerrc" ) );
00053   c.setGroup( "Time & Date" );
00054   TQString 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, TQT_SIGNAL( timeout() ), TQT_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     TQString group( TQString( "Incidence-%1" ).arg( i ) );
00075     config->setGroup( group );
00076     TQString uid = config->readEntry( "UID" );
00077     TQDateTime dt = config->readDateTimeEntry( "RemindAt" );
00078     if ( !uid.isEmpty() ) {
00079       Incidence *i = mCalendar->incidence( uid );
00080       if ( i && !i->alarms().isEmpty() ) {
00081         createReminder( mCalendar, i, dt, TQString() );
00082       }
00083     }
00084     config->deleteGroup( group );
00085   }
00086   config->setGroup( "General" );
00087   if (numReminders) {
00088      config->writeEntry( "Reminders", 0 );
00089      config->sync();
00090   }
00091 
00092   checkAlarms();
00093   mCheckTimer.start( 1000 * interval );  // interval in seconds
00094 }
00095 
00096 KOAlarmClient::~KOAlarmClient()
00097 {
00098   delete mCalendar;
00099   delete mDocker;
00100   delete mDialog;
00101 }
00102 
00103 void KOAlarmClient::checkAlarms()
00104 {
00105   KConfig *cfg = kapp->config();
00106 
00107   cfg->setGroup( "General" );
00108   if ( !cfg->readBoolEntry( "Enabled", true ) ) return;
00109 
00110   TQDateTime from = mLastChecked.addSecs( 1 );
00111   mLastChecked = TQDateTime::currentDateTime();
00112 
00113   kdDebug(5891) << "Check: " << from.toString() << " - " << mLastChecked.toString() << endl;
00114 
00115   TQValueList<Alarm *> alarms = mCalendar->alarms( from, mLastChecked );
00116 
00117   TQValueList<Alarm *>::ConstIterator it;
00118   for( it = alarms.begin(); it != alarms.end(); ++it ) {
00119     kdDebug(5891) << "REMINDER: " << (*it)->parent()->summary() << endl;
00120     Incidence *incidence = mCalendar->incidence( (*it)->parent()->uid() );
00121     createReminder( mCalendar, incidence, from, (*it)->text() );
00122   }
00123 }
00124 
00125 void KOAlarmClient::createReminder( KCal::CalendarResources *calendar,
00126                                     KCal::Incidence *incidence,
00127                                     const TQDateTime &dt,
00128                                     const TQString &displayText )
00129 {
00130   if ( !incidence )
00131     return;
00132 
00133   if ( !mDialog ) {
00134     mDialog = new AlarmDialog( calendar );
00135     connect( mDialog, TQT_SIGNAL(reminderCount(int)), mDocker, TQT_SLOT(slotUpdate(int)) );
00136     connect( mDocker, TQT_SIGNAL(suspendAllSignal()), mDialog, TQT_SLOT(suspendAll()) );
00137     connect( mDocker, TQT_SIGNAL(dismissAllSignal()), mDialog, TQT_SLOT(dismissAll()) );
00138     connect( this, TQT_SIGNAL( saveAllSignal() ), mDialog, TQT_SLOT( slotSave() ) );
00139   }
00140 
00141   mDialog->addIncidence( incidence, dt, displayText );
00142   mDialog->wakeUp();
00143   saveLastCheckTime();
00144 }
00145 
00146 void KOAlarmClient::slotQuit()
00147 {
00148   emit saveAllSignal();
00149   saveLastCheckTime();
00150   quit();
00151 }
00152 
00153 void KOAlarmClient::saveLastCheckTime()
00154 {
00155   KConfigGroup cg( KGlobal::config(), "Alarms");
00156   cg.writeEntry( "CalendarsLastChecked", mLastChecked );
00157   KGlobal::config()->sync();
00158 }
00159 
00160 void KOAlarmClient::quit()
00161 {
00162   kdDebug(5890) << "KOAlarmClient::quit()" << endl;
00163   kapp->quit();
00164 }
00165 
00166 bool KOAlarmClient::commitData( TQSessionManager& )
00167 {
00168   emit saveAllSignal();
00169   saveLastCheckTime();
00170   return true;
00171 }
00172 
00173 void KOAlarmClient::forceAlarmCheck()
00174 {
00175   checkAlarms();
00176   saveLastCheckTime();
00177 }
00178 
00179 void KOAlarmClient::dumpDebug()
00180 {
00181   KConfig *cfg = kapp->config();
00182 
00183   cfg->setGroup( "Alarms" );
00184   TQDateTime lastChecked = cfg->readDateTimeEntry( "CalendarsLastChecked" );
00185 
00186   kdDebug(5890) << "Last Check: " << lastChecked << endl;
00187 }
00188 
00189 TQStringList KOAlarmClient::dumpAlarms()
00190 {
00191   TQDateTime start = TQDateTime( TQDateTime::currentDateTime().date(),
00192                                TQTime( 0, 0 ) );
00193   TQDateTime end = start.addDays( 1 ).addSecs( -1 );
00194 
00195   TQStringList lst;
00196   // Don't translate, this is for debugging purposes.
00197   lst << TQString("AlarmDeamon::dumpAlarms() from ") + start.toString()+ " to " +
00198          end.toString();
00199 
00200   TQValueList<Alarm*> alarms = mCalendar->alarms( start, end );
00201   TQValueList<Alarm*>::ConstIterator it;
00202   for( it = alarms.begin(); it != alarms.end(); ++it ) {
00203     Alarm *a = *it;
00204     lst << TQString("  ") + a->parent()->summary() + " ("
00205               + a->time().toString() + ")";
00206   }
00207 
00208   return lst;
00209 }
00210 
00211 void KOAlarmClient::debugShowDialog()
00212 {
00213 //   showAlarmDialog();
00214 }
00215 
00216 #include "koalarmclient.moc"