korganizer

alarmdialog.cpp

00001 /*
00002     This file is part of the KOrganizer alarm daemon.
00003 
00004     Copyright (c) 2000,2003 Cornelius Schumacher <schumacher@kde.org>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019 
00020     As a special exception, permission is given to link this program
00021     with any edition of Qt, and distribute the resulting executable,
00022     without including the source code for Qt in the source distribution.
00023 */
00024 
00025 #include <qhbox.h>
00026 #include <qvbox.h>
00027 #include <qlabel.h>
00028 #include <qfile.h>
00029 #include <qspinbox.h>
00030 #include <qlayout.h>
00031 #include <qpushbutton.h>
00032 #include <qcstring.h>
00033 #include <qdatastream.h>
00034 
00035 #include <kapplication.h>
00036 #include <kiconloader.h>
00037 #include <dcopclient.h>
00038 #include <klocale.h>
00039 #include <kprocess.h>
00040 #include <kaudioplayer.h>
00041 #include <kdebug.h>
00042 #include <kmessagebox.h>
00043 #include <knotifyclient.h>
00044 #include <kcombobox.h>
00045 #include <kwin.h>
00046 #include <klockfile.h>
00047 
00048 #include <libkcal/event.h>
00049 
00050 #include "koeventviewer.h"
00051 
00052 #include "alarmdialog.h"
00053 #include "alarmdialog.moc"
00054 
00055 AlarmDialog::AlarmDialog( QWidget *parent, const char *name )
00056   : KDialogBase( Plain, WType_TopLevel | WStyle_Customize | WStyle_StaysOnTop |
00057                  WStyle_DialogBorder,
00058                  parent, name, false, i18n("Reminder"), Ok | User1 | User2/* | User3*/, User1/*3*/,
00059                  false, i18n("Suspend"), i18n("Edit...") ),
00060                  mSuspendTimer(this)
00061 {
00062   KGlobal::iconLoader()->addAppDir( "kdepim" );
00063 
00064   QWidget *topBox = plainPage();
00065   QBoxLayout *topLayout = new QVBoxLayout( topBox );
00066   topLayout->setSpacing( spacingHint() );
00067 
00068   QLabel *label = new QLabel( i18n("The following events triggered reminders:"),
00069                               topBox );
00070   topLayout->addWidget( label );
00071 
00072   mEventViewer = new KOEventViewer( topBox );
00073   topLayout->addWidget( mEventViewer );
00074 
00075   QHBox *suspendBox = new QHBox( topBox );
00076   suspendBox->setSpacing( spacingHint() );
00077   topLayout->addWidget( suspendBox );
00078 
00079   QLabel *l = new QLabel( i18n("Suspend &duration:"), suspendBox );
00080   mSuspendSpin = new QSpinBox( 1, 9999, 1, suspendBox );
00081   mSuspendSpin->setValue( 5 );  // default suspend duration
00082   l->setBuddy( mSuspendSpin );
00083 
00084   mSuspendUnit = new KComboBox( suspendBox );
00085   mSuspendUnit->insertItem( i18n("minute(s)") );
00086   mSuspendUnit->insertItem( i18n("hour(s)") );
00087   mSuspendUnit->insertItem( i18n("day(s)") );
00088   mSuspendUnit->insertItem( i18n("week(s)") );
00089 
00090   // showButton( User2/*3*/, false );
00091 
00092   setMinimumSize( 300, 200 );
00093 }
00094 
00095 AlarmDialog::~AlarmDialog()
00096 {
00097   delete mIncidence;
00098 }
00099 
00100 void AlarmDialog::setIncidence( Incidence *incidence )
00101 {
00102   mIncidence = incidence->clone();
00103   mEventViewer->appendIncidence( mIncidence );
00104 }
00105 
00106 void AlarmDialog::setRemindAt( QDateTime dt )
00107 {
00108   mRemindAt = dt;
00109 }
00110 
00111 void AlarmDialog::slotOk()
00112 {
00113   accept();
00114   emit finishedSignal( this );
00115 }
00116 
00117 void AlarmDialog::slotUser1()
00118 {
00119   if ( !isVisible() )
00120     return;
00121 
00122   int unit=1;
00123   switch (mSuspendUnit->currentItem()) {
00124     case 3: // weeks
00125       unit *=  7;
00126     case 2: // days
00127       unit *= 24;
00128     case 1: // hours
00129       unit *= 60;
00130     case 0: // minutes
00131       unit *= 60;
00132     default:
00133       break;
00134   }
00135 
00136   setTimer( unit * mSuspendSpin->value() );
00137   accept();
00138 }
00139 
00140 void AlarmDialog::setTimer( int seconds )
00141 {
00142   connect( &mSuspendTimer, SIGNAL( timeout() ), SLOT( show() ) );
00143   mSuspendTimer.start( 1000 * seconds, true );
00144   mRemindAt = QDateTime::currentDateTime();
00145   mRemindAt = mRemindAt.addSecs( seconds );
00146 }
00147 
00148 void AlarmDialog::slotUser2()
00149 {
00150   if ( !kapp->dcopClient()->isApplicationRegistered( "korganizer" ) ) {
00151     if ( kapp->startServiceByDesktopName( "korganizer", QString::null ) )
00152       KMessageBox::error( 0, i18n("Could not start KOrganizer.") );
00153   }
00154 
00155   kapp->dcopClient()->send( "korganizer", "KOrganizerIface",
00156                             "editIncidence(QString)",
00157                              mIncidence->uid() );
00158 
00159   // get desktop # where korganizer (or kontact) runs
00160   QByteArray replyData;
00161   QCString object, replyType;
00162   object = kapp->dcopClient()->isApplicationRegistered( "kontact" ) ?
00163            "kontact-mainwindow#1" : "KOrganizer MainWindow";
00164   if (!kapp->dcopClient()->call( "korganizer", object,
00165                             "getWinID()", 0, replyType, replyData, true, -1 ) ) {
00166   }
00167 
00168   if ( replyType == "int" ) {
00169     int desktop, window;
00170     QDataStream ds( replyData, IO_ReadOnly );
00171     ds >> window;
00172     desktop = KWin::windowInfo( window ).desktop();
00173 
00174     if ( KWin::currentDesktop() == desktop ) {
00175       KWin::iconifyWindow( winId(), false );
00176     }
00177     else
00178       KWin::setCurrentDesktop( desktop );
00179 
00180     KWin::activateWindow( KWin::transientFor( window ) );
00181   }
00182 }
00183 
00184 void AlarmDialog::show()
00185 {
00186   KDialogBase::show();
00187   KWin::setState( winId(), NET::KeepAbove );
00188   KWin::setOnAllDesktops( winId(), true );
00189   eventNotification();
00190 }
00191 
00192 void AlarmDialog::eventNotification()
00193 {
00194   bool beeped = false;
00195   Alarm::List alarms = mIncidence->alarms();
00196   Alarm::List::ConstIterator it;
00197   for ( it = alarms.begin(); it != alarms.end(); ++it ) {
00198     Alarm *alarm = *it;
00199 // FIXME: Check whether this should be done for all multiple alarms
00200     if (alarm->type() == Alarm::Procedure) {
00201 // FIXME: Add a message box asking whether the procedure should really be executed
00202       kdDebug(5890) << "Starting program: '" << alarm->programFile() << "'" << endl;
00203       KProcess proc;
00204       proc << QFile::encodeName(alarm->programFile());
00205       proc.start(KProcess::DontCare);
00206     }
00207     else if (alarm->type() == Alarm::Audio) {
00208       beeped = true;
00209       KAudioPlayer::play(QFile::encodeName(alarm->audioFile()));
00210     }
00211   }
00212 
00213   if ( !beeped ) {
00214     KNotifyClient::beep();
00215   }
00216 }
00217 
00218 void AlarmDialog::wakeUp()
00219 {
00220   if ( mRemindAt <= QDateTime::currentDateTime() )
00221     show();
00222   else
00223     setTimer( QDateTime::currentDateTime().secsTo( mRemindAt ) );
00224 }
00225 
00226 void AlarmDialog::slotSave()
00227 {
00228   KConfig *config = kapp->config();
00229   KLockFile::Ptr lock = config->lockFile();
00230   if ( lock.data()->lock() != KLockFile::LockOK )
00231     return;
00232 
00233   config->setGroup( "General" );
00234   int numReminders = config->readNumEntry("Reminders", 0);
00235   config->writeEntry( "Reminders", ++numReminders );
00236 
00237   config->setGroup( QString("Incidence-%1").arg(numReminders) );
00238   config->writeEntry( "UID", mIncidence->uid() );
00239   config->writeEntry( "RemindAt", mRemindAt );
00240   config->sync();
00241   lock.data()->unlock();
00242 }
00243 
KDE Home | KDE Accessibility Home | Description of Access Keys