00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "koalarmclient.h"
00029 #include "korgacadaptor.h"
00030 #include "alarmdockwindow.h"
00031 #include "alarmdialog.h"
00032
00033 #include <kcal/calendarresources.h>
00034
00035 #include <kstandarddirs.h>
00036 #include <kdebug.h>
00037 #include <klocale.h>
00038 #include <kdatetime.h>
00039 #include <kapplication.h>
00040 #include <kwindowsystem.h>
00041 #include <kconfiggroup.h>
00042 #include <kglobal.h>
00043
00044 #include <QPushButton>
00045 #include <QtDBus/QtDBus>
00046
00047 KOAlarmClient::KOAlarmClient( QObject *parent )
00048 : QObject( parent ),
00049 mDialog( 0 )
00050 {
00051 new KOrgacAdaptor( this );
00052 QDBusConnection::sessionBus().registerObject( "/ac", this );
00053 kDebug();
00054
00055 KConfig korgConfig( KStandardDirs::locate( "config", "korganizerrc" ) );
00056 KConfigGroup generalGroup( &korgConfig, "General" );
00057 bool showDock = generalGroup.readEntry( "ShowReminderDaemon", true );
00058
00059 mDocker = new AlarmDockWindow;
00060 if ( showDock ) {
00061 mDocker->show();
00062 } else {
00063 mDocker->hide();
00064 }
00065
00066 connect( this, SIGNAL(reminderCount(int)), mDocker, SLOT(slotUpdate(int)) );
00067 connect( mDocker, SIGNAL(quitSignal()), SLOT(slotQuit()) );
00068
00069 KConfigGroup timedateGroup( &korgConfig, "Time & Date" );
00070 QString tz = timedateGroup.readEntry( "TimeZoneId" );
00071 kDebug() << "TimeZone:" << tz;
00072
00073 mCalendar = new CalendarResources( tz );
00074 mCalendar->readConfig();
00075 mCalendar->load();
00076
00077 connect( &mCheckTimer, SIGNAL(timeout()), SLOT(checkAlarms()) );
00078
00079 KConfigGroup alarmGroup( KGlobal::config(), "Alarms" );
00080 int interval = alarmGroup.readEntry( "Interval", 60 );
00081 kDebug() << "KOAlarmClient check interval:" << interval << "seconds.";
00082 mLastChecked = alarmGroup.readEntry( "CalendarsLastChecked", QDateTime() );
00083
00084
00085 KConfigGroup genGroup( KGlobal::config(), "General" );
00086 int numReminders = genGroup.readEntry( "Reminders", 0 );
00087 for ( int i=1; i<=numReminders; ++i ) {
00088 QString group( QString( "Incidence-%1" ).arg( i ) );
00089 KConfigGroup *incGroup = new KConfigGroup( KGlobal::config(), group );
00090 QString uid = incGroup->readEntry( "UID" );
00091 QDateTime dt = incGroup->readEntry( "RemindAt", QDateTime() );
00092 if ( !uid.isEmpty() ) {
00093 createReminder( mCalendar->incidence( uid ), dt );
00094 }
00095 delete incGroup;
00096 }
00097 if ( numReminders ) {
00098 genGroup.writeEntry( "Reminders", 0 );
00099 genGroup.sync();
00100 }
00101
00102 checkAlarms();
00103 mCheckTimer.start( 1000 * interval );
00104 }
00105
00106 KOAlarmClient::~KOAlarmClient()
00107 {
00108 delete mCalendar;
00109 delete mDocker;
00110 delete mDialog;
00111 }
00112
00113 void KOAlarmClient::checkAlarms()
00114 {
00115 KConfigGroup cfg( KGlobal::config(), "General" );
00116
00117 if ( !cfg.readEntry( "Enabled", true ) ) {
00118 return;
00119 }
00120
00121 QDateTime from = mLastChecked.addSecs( 1 );
00122 mLastChecked = QDateTime::currentDateTime();
00123
00124 kDebug(5891) << "Check:" << from.toString() << " -" << mLastChecked.toString();
00125
00126 QList<Alarm *>alarms = mCalendar->alarms( KDateTime( from, KDateTime::LocalZone ),
00127 KDateTime( mLastChecked, KDateTime::LocalZone ) );
00128
00129 QList<Alarm *>::ConstIterator it;
00130 for ( it = alarms.begin(); it != alarms.end(); ++it ) {
00131 kDebug(5891) << "REMINDER:" << (*it)->parent()->summary();
00132 Incidence *incidence = mCalendar->incidence( (*it)->parent()->uid() );
00133 createReminder( incidence, QDateTime::currentDateTime() );
00134 }
00135 }
00136
00137 void KOAlarmClient::createReminder( KCal::Incidence *incidence,
00138 const QDateTime &dt )
00139 {
00140 if ( !incidence ) {
00141 return;
00142 }
00143
00144 if ( !mDialog ) {
00145 mDialog = new AlarmDialog();
00146 connect( mDialog, SIGNAL(reminderCount(int)), mDocker, SLOT(slotUpdate(int)) );
00147 connect( mDocker, SIGNAL(suspendAllSignal()), mDialog, SLOT(suspendAll()) );
00148 connect( mDocker, SIGNAL(dismissAllSignal()), mDialog, SLOT(dismissAll()) );
00149 connect( this, SIGNAL(saveAllSignal()), mDialog, SLOT(slotSave()) );
00150 }
00151
00152 mDialog->addIncidence( incidence, dt );
00153 mDialog->wakeUp();
00154 saveLastCheckTime();
00155 }
00156
00157 void KOAlarmClient::slotQuit()
00158 {
00159 emit saveAllSignal();
00160 saveLastCheckTime();
00161 quit();
00162 }
00163
00164 void KOAlarmClient::saveLastCheckTime()
00165 {
00166 KConfigGroup cg( KGlobal::config(), "Alarms" );
00167 cg.writeEntry( "CalendarsLastChecked", mLastChecked );
00168 KGlobal::config()->sync();
00169 }
00170
00171 void KOAlarmClient::quit()
00172 {
00173 kDebug();
00174 kapp->quit();
00175 }
00176
00177 bool KOAlarmClient::commitData( QSessionManager & )
00178 {
00179 emit saveAllSignal();
00180 saveLastCheckTime();
00181 return true;
00182 }
00183
00184 void KOAlarmClient::forceAlarmCheck()
00185 {
00186 checkAlarms();
00187 saveLastCheckTime();
00188 }
00189
00190 void KOAlarmClient::dumpDebug()
00191 {
00192 KConfigGroup cfg( KGlobal::config(), "Alarms" );
00193
00194 QDateTime lastChecked = cfg.readEntry( "CalendarsLastChecked", QDateTime() );
00195
00196 kDebug() << "Last Check:" << lastChecked;
00197 }
00198
00199 QStringList KOAlarmClient::dumpAlarms()
00200 {
00201 KDateTime start = KDateTime( QDateTime::currentDateTime().date(),
00202 QTime( 0, 0 ), KDateTime::LocalZone );
00203 KDateTime end = start.addDays( 1 ).addSecs( -1 );
00204
00205 QStringList lst;
00206
00207 lst << QString( "AlarmDeamon::dumpAlarms() from " ) + start.toString() + " to " +
00208 end.toString();
00209
00210 QList<Alarm *> alarms = mCalendar->alarms( start, end );
00211 QList<Alarm *>::ConstIterator it;
00212 for ( it = alarms.begin(); it != alarms.end(); ++it ) {
00213 Alarm *a = *it;
00214 lst << QString( " " ) + a->parent()->summary() + " (" + a->time().toString() + ')';
00215 }
00216
00217 return lst;
00218 }
00219
00220 void KOAlarmClient::debugShowDialog()
00221 {
00222
00223 }
00224
00225 void KOAlarmClient::hide()
00226 {
00227 mDocker->hide();
00228 }
00229
00230 void KOAlarmClient::show()
00231 {
00232 mDocker->show();
00233 }
00234
00235 #include "koalarmclient.moc"