korganizer Library API Documentation

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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 ),
00044     mSuspendTimer( this )
00045 {
00046   kdDebug(5890) << "KOAlarmClient::KOAlarmClient()" << endl;
00047 
00048   mDocker = new AlarmDockWindow;
00049   mDocker->show();
00050 
00051   mAlarmDialog = new AlarmDialog;
00052   connect( mAlarmDialog, SIGNAL( suspendSignal( int ) ),
00053            SLOT( suspend( int ) ) );
00054 
00055   KConfig c( locate( "config", "korganizerrc" ) );
00056   c.setGroup( "Time & Date" );
00057   QString tz = c.readEntry( "TimeZoneId" );
00058   kdDebug(5890) << "TimeZone: " << tz << endl;
00059 
00060   mCalendar = new CalendarResources( tz );
00061   mCalendar->readConfig();
00062   mCalendar->load();
00063 
00064   connect( &mCheckTimer, SIGNAL( timeout() ), SLOT( checkAlarms() ) );
00065 
00066   KConfig *cfg = KGlobal::config();
00067   cfg->setGroup( "Alarms" );
00068   int interval = cfg->readNumEntry( "Interval", 60 );
00069   kdDebug(5890) << "KOAlarmClient check interval: " << interval << " seconds."
00070                 << endl;
00071 
00072   mCheckTimer.start( 1000 * interval );  // interval in seconds
00073 }
00074 
00075 KOAlarmClient::~KOAlarmClient()
00076 {
00077   delete mCalendar;
00078   delete mDocker;
00079 }
00080 
00081 void KOAlarmClient::checkAlarms()
00082 {
00083   KConfig *cfg = KGlobal::config();
00084 
00085   cfg->setGroup( "General" );
00086   if ( !cfg->readBoolEntry( "Enabled", true ) ) return;
00087 
00088   cfg->setGroup( "Alarms" );
00089   QDateTime lastChecked = cfg->readDateTimeEntry( "CalendarsLastChecked" );
00090   QDateTime from = lastChecked.addSecs( 1 );
00091   QDateTime to = QDateTime::currentDateTime();
00092 
00093   kdDebug(5891) << "Check: " << from.toString() << " - " << to.toString() << endl;
00094 
00095   QValueList<Alarm *> alarms = mCalendar->alarms( from, to );
00096   
00097   bool newEvents = false;
00098   QValueList<Alarm *>::ConstIterator it;
00099   for( it = alarms.begin(); it != alarms.end(); ++it ) {
00100     kdDebug(5891) << "ALARM: " << (*it)->parent()->summary() << endl;
00101     Incidence *incidence = mCalendar->incidence( (*it)->parent()->uid() );
00102     if ( incidence->type() == "Event" ) {
00103       mAlarmDialog->appendEvent( static_cast<Event *>(incidence) );
00104       newEvents = true;
00105     } else if ( incidence->type() == "Todo" ) {
00106       mAlarmDialog->appendTodo( static_cast<Todo *>(incidence) );
00107       newEvents = true;
00108     }
00109   }
00110   if ( newEvents ) {
00111     showAlarmDialog();
00112   }
00113 
00114   cfg->writeEntry( "CalendarsLastChecked", to );
00115 
00116   cfg->sync();
00117 }
00118 
00119 void KOAlarmClient::suspend( int seconds )
00120 {
00121 //  kdDebug(5890) << "KOAlarmClient::suspend() " << minutes << " minutes" << endl;
00122   connect( &mSuspendTimer, SIGNAL( timeout() ), SLOT( showAlarmDialog() ) );
00123   mSuspendTimer.start( 1000 * seconds, true );
00124 }
00125 
00126 void KOAlarmClient::showAlarmDialog()
00127 {
00128   mAlarmDialog->show();
00129   mAlarmDialog->raise();
00130   KWin::forceActiveWindow( mAlarmDialog->winId() );
00131   mAlarmDialog->actionButton( KDialogBase::Ok )->setFocus();
00132   mAlarmDialog->eventNotification();
00133 }
00134 
00135 void KOAlarmClient::quit()
00136 {
00137   kdDebug(5890) << "KOAlarmClient::quit()" << endl;
00138   kapp->quit();
00139 }
00140 
00141 void KOAlarmClient::forceAlarmCheck()
00142 {
00143   checkAlarms();
00144 }
00145 
00146 void KOAlarmClient::dumpDebug()
00147 {
00148   KConfig *cfg = KGlobal::config();
00149 
00150   cfg->setGroup( "Alarms" );
00151   QDateTime lastChecked = cfg->readDateTimeEntry( "CalendarsLastChecked" );
00152 
00153   kdDebug(5890) << "Last Check: " << lastChecked << endl;
00154 }
00155 
00156 QStringList KOAlarmClient::dumpAlarms()
00157 {
00158   QDateTime start = QDateTime( QDateTime::currentDateTime().date(),
00159                                QTime( 0, 0 ) );
00160   QDateTime end = start.addDays( 1 ).addSecs( -1 );
00161 
00162   QStringList lst;
00163   // Don't translate, this is for debugging purposes.
00164   lst << QString("AlarmDeamon::dumpAlarms() from ") + start.toString()+ " to " +
00165          end.toString();
00166 
00167   QValueList<Alarm*> alarms = mCalendar->alarms( start, end );
00168   QValueList<Alarm*>::ConstIterator it;
00169   for( it = alarms.begin(); it != alarms.end(); ++it ) {
00170     Alarm *a = *it;
00171     lst << QString("  ") + a->parent()->summary() + " ("
00172               + a->time().toString() + ")";
00173   }
00174 
00175   return lst;
00176 }
00177 
00178 void KOAlarmClient::debugShowDialog()
00179 {
00180   showAlarmDialog();
00181 }
00182 
00183 #include "koalarmclient.moc"
KDE Logo
This file is part of the documentation for korganizer Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Dec 21 14:25:44 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003