korganizer Library API Documentation

koeventeditor.cpp

00001 /*
00002     This file is part of KOrganizer.
00003 
00004     Copyright (c) 2001, 2002, 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 <qtooltip.h>
00026 #include <qframe.h>
00027 #include <qpixmap.h>
00028 #include <qlayout.h>
00029 #include <qwidgetstack.h>
00030 
00031 #include <kiconloader.h>
00032 #include <kdebug.h>
00033 #include <klocale.h>
00034 #include <kmessagebox.h>
00035 #include <libkcal/calendarresources.h>
00036 #include <libkcal/resourcecalendar.h>
00037 
00038 #include <libkdepim/categoryselectdialog.h>
00039 #include <libkcal/calendarlocal.h>
00040 
00041 #include "koprefs.h"
00042 #include "koeditordetails.h"
00043 #include "koeditorattachments.h"
00044 #include "koeditorgantt.h"
00045 #include "kogroupware.h"
00046 #include "kodialogmanager.h"
00047 
00048 #include "koeventeditor.h"
00049 
00050 KOEventEditor::KOEventEditor( Calendar *calendar, QWidget *parent ) :
00051   KOIncidenceEditor( i18n("Edit Event"), calendar, parent ), mGantt( 0 )
00052 {
00053   mEvent = 0;
00054 }
00055 
00056 KOEventEditor::~KOEventEditor()
00057 {
00058   emit dialogClose( mEvent );
00059 }
00060 
00061 void KOEventEditor::init()
00062 {
00063   setupGeneral();
00064   setupAttendeesTab();
00065   setupRecurrence();
00066   setupAttachmentsTab();
00067 
00068   if( KOPrefs::instance()->mUseGroupwareCommunication ) {
00069     setupGanttTab();
00070     mDetails->setGanttWidget( mGantt );
00071   }
00072 
00073   // Propagate date time settings to recurrence tab
00074   connect(mGeneral,SIGNAL(dateTimesChanged(QDateTime,QDateTime)),
00075           mRecurrence,SLOT(setDateTimes(QDateTime,QDateTime)));
00076   connect(mGeneral,SIGNAL(dateTimeStrChanged(const QString &)),
00077           mRecurrence,SLOT(setDateTimeStr(const QString &)));
00078   if( mGantt )
00079     connect(mGantt,SIGNAL(dateTimesChanged(QDateTime,QDateTime)),
00080         mRecurrence,SLOT(setDateTimes(QDateTime,QDateTime)));
00081 
00082   // Propagate date time settings to gantt tab and back
00083   if( mGantt ) {
00084     connect(mGeneral,SIGNAL(dateTimesChanged(QDateTime,QDateTime)),
00085         mGantt,SLOT(setDateTimes(QDateTime,QDateTime)));
00086     connect(mGantt,SIGNAL(dateTimesChanged(QDateTime,QDateTime)),
00087         mGeneral,SLOT(setDateTimes(QDateTime,QDateTime)));
00088   }
00089 
00090   // Category dialog
00091   connect(mGeneral,SIGNAL(openCategoryDialog()),mCategoryDialog,SLOT(show()));
00092   connect(mCategoryDialog,SIGNAL(categoriesSelected(const QString &)),
00093           mGeneral,SLOT(setCategories(const QString &)));
00094 
00095   connect(mGeneral,SIGNAL(focusReceivedSignal()),SIGNAL(focusReceivedSignal()));
00096 }
00097 
00098 void KOEventEditor::reload()
00099 {
00100   if ( mEvent ) readEvent( mEvent );
00101 }
00102 
00103 void KOEventEditor::setupGeneral()
00104 {
00105   mGeneral = new KOEditorGeneralEvent( this );
00106 
00107   if( KOPrefs::instance()->mCompactDialogs ) {
00108     QFrame *topFrame = addPage(i18n("General"));
00109 
00110     QBoxLayout *topLayout = new QVBoxLayout(topFrame);
00111     topLayout->setSpacing(spacingHint());
00112 
00113     mGeneral->initHeader(topFrame,topLayout);
00114     mGeneral->initTime(topFrame,topLayout);
00115 //    QBoxLayout *alarmLineLayout = new QHBoxLayout(topLayout);
00116     mGeneral->initAlarm(topFrame,topLayout);
00117     mGeneral->enableAlarm( false );
00118     mGeneral->initCategories( topFrame, topLayout );
00119 
00120     topLayout->addStretch( 1 );
00121 
00122     QFrame *topFrame2 = addPage(i18n("Details"));
00123 
00124     QBoxLayout *topLayout2 = new QVBoxLayout(topFrame2);
00125     topLayout2->setSpacing(spacingHint());
00126 
00127     mGeneral->initClass(topFrame2,topLayout2);
00128     mGeneral->initSecrecy( topFrame2, topLayout2 );
00129     mGeneral->initDescription(topFrame2,topLayout2);
00130   } else {
00131     QFrame *topFrame = addPage(i18n("&General"));
00132 
00133     QBoxLayout *topLayout = new QVBoxLayout(topFrame);
00134     topLayout->setSpacing(spacingHint());
00135 
00136     mGeneral->initHeader(topFrame,topLayout);
00137     mGeneral->initTime(topFrame,topLayout);
00138     QBoxLayout *alarmLineLayout = new QHBoxLayout(topLayout);
00139     mGeneral->initAlarm(topFrame,alarmLineLayout);
00140     mGeneral->initClass(topFrame,alarmLineLayout);
00141     mGeneral->initDescription(topFrame,topLayout);
00142     QBoxLayout *detailsLayout = new QHBoxLayout(topLayout);
00143     mGeneral->initCategories( topFrame, detailsLayout );
00144     mGeneral->initSecrecy( topFrame, detailsLayout );
00145   }
00146 
00147   mGeneral->finishSetup();
00148 }
00149 
00150 void KOEventEditor::setupRecurrence()
00151 {
00152   QFrame *topFrame = addPage( i18n("Rec&urrence") );
00153 
00154   QBoxLayout *topLayout = new QVBoxLayout( topFrame );
00155 
00156   mRecurrence = new KOEditorRecurrence( topFrame );
00157   topLayout->addWidget( mRecurrence );
00158 }
00159 
00160 void KOEventEditor::setupGanttTab()
00161 {
00162   QFrame* frame = addPage( i18n("&Gantt") );
00163   mGantt = new KOEditorGantt( spacingHint(), frame );
00164   ( new QVBoxLayout( frame ) )->addWidget( mGantt );
00165 }
00166 
00167 void KOEventEditor::editEvent(Event *event)
00168 {
00169   init();
00170 
00171   mEvent = event;
00172   readEvent(mEvent);
00173 }
00174 
00175 void KOEventEditor::newEvent( QDateTime from, QDateTime to, bool allDay )
00176 {
00177   init();
00178 
00179   mEvent = 0;
00180   setDefaults(from,to,allDay);
00181 }
00182 
00183 void KOEventEditor::newEvent( const QString &text )
00184 {
00185   init();
00186 
00187   mEvent = 0;
00188 
00189   loadDefaults();
00190 
00191   mGeneral->setDescription( text );
00192 
00193   int pos = text.find( "\n" );
00194   if ( pos > 0 ) {
00195     mGeneral->setSummary( text.left( pos ) );
00196     mGeneral->setDescription( text );
00197   } else {
00198     mGeneral->setSummary( text );
00199   }
00200 }
00201 
00202 void KOEventEditor::newEvent( const QString &summary,
00203                               const QString &description,
00204                               const QString &attachment )
00205 {
00206   init();
00207   
00208   mEvent = 0;
00209 
00210   loadDefaults();
00211 
00212   mGeneral->setSummary( summary );
00213   mGeneral->setDescription( description );
00214 
00215   if ( !attachment.isEmpty() ) {
00216     mAttachments->addAttachment( attachment );
00217   }
00218 }
00219 
00220 void KOEventEditor::loadDefaults()
00221 {
00222   int fmt = KOPrefs::instance()->mStartTime;
00223 
00224   QDateTime from(QDate::currentDate(), QTime(fmt,0,0));
00225   QDateTime to(QDate::currentDate(),
00226                QTime(fmt+KOPrefs::instance()->mDefaultDuration,0,0));
00227 
00228   setDefaults(from,to,false);
00229 }
00230 
00231 bool KOEventEditor::processInput()
00232 {
00233   if ( !validateInput() ) return false;
00234 
00235   if ( mEvent ) {
00236     bool rc = true;
00237     Event *event = mEvent->clone();
00238     Event *oldEvent = mEvent->clone();
00239     writeEvent( event );
00240 
00241     if( *mEvent == *event )
00242       // Don't do anything
00243       kdDebug(5850) << "Event not changed\n";
00244     else {
00245       kdDebug(5850) << "Event changed\n";
00246       int revision = event->revision();
00247       event->setRevision( revision + 1 );
00248       if( !KOPrefs::instance()->mUseGroupwareCommunication ||
00249       KOGroupware::instance()->sendICalMessage( this, KCal::Scheduler::Request, event ) )
00250       {
00251     // Accept the event changes
00252     writeEvent( mEvent );
00253     mEvent->setRevision( revision + 1 );
00254     emit eventChanged( oldEvent, mEvent );
00255       } else {
00256     // Revert the changes
00257     event->setRevision( revision );
00258     rc = false;
00259       }
00260     }
00261     delete event;
00262     return rc;
00263   } else {
00264     mEvent = new Event;
00265     mEvent->setOrganizer( KOPrefs::instance()->email() );
00266     writeEvent( mEvent );
00267     if( !KOPrefs::instance()->mUseGroupwareCommunication ||
00268     KOGroupware::instance()->sendICalMessage( this, KCal::Scheduler::Request, mEvent ) ) {
00269       if ( mCalendar->addEvent( mEvent ) ) {
00270         emit eventAdded( mEvent );
00271       } else {
00272         KODialogManager::errorSaveEvent( this );
00273         delete mEvent;
00274         mEvent = 0;
00275         return false;
00276       }
00277     } else {
00278       return false;
00279     }
00280   }
00281 
00282   return true;
00283 }
00284 
00285 void KOEventEditor::processCancel()
00286 {
00287   kdDebug() << "KOEventEditor::processCancel()" << endl;
00288 
00289   if ( mEvent ) {
00290     emit editCanceled( mEvent );
00291   }
00292 }
00293 
00294 void KOEventEditor::deleteEvent()
00295 {
00296   kdDebug(5850) << "Delete event" << endl;
00297 
00298   if (mEvent) {
00299     if (KOPrefs::instance()->mConfirm && (!KOPrefs::instance()->mUseGroupwareCommunication ||
00300                       KOPrefs::instance()->email() == mEvent->organizer())) {
00301       switch (msgItemDelete()) {
00302         case KMessageBox::Continue: // OK
00303           emit eventToBeDeleted(mEvent);
00304           emit dialogClose(mEvent);
00305           mCalendar->deleteEvent(mEvent);
00306           emit eventDeleted(mEvent);
00307           reject();
00308           break;
00309       }
00310     }
00311     else {
00312       emit eventToBeDeleted(mEvent);
00313       emit dialogClose(mEvent);
00314       mCalendar->deleteEvent(mEvent);
00315       emit eventDeleted(mEvent);
00316       reject();
00317     }
00318   } else {
00319     reject();
00320   }
00321 }
00322 
00323 void KOEventEditor::setDefaults( QDateTime from, QDateTime to, bool allDay )
00324 {
00325   mGeneral->setDefaults( from, to, allDay );
00326   mDetails->setDefaults();
00327   mAttachments->setDefaults();
00328   mRecurrence->setDefaults( from, to, allDay );
00329 }
00330 
00331 void KOEventEditor::readEvent( Event *event, bool tmpl )
00332 {
00333   mGeneral->readEvent( event, tmpl );
00334   mDetails->readEvent( event );
00335   mRecurrence->readEvent( event );
00336   mAttachments->readIncidence( event );
00337   if( mGantt ) mGantt->readEvent( event );
00338 
00339   // categories
00340   mCategoryDialog->setSelected( event->categories() );
00341 }
00342 
00343 void KOEventEditor::writeEvent( Event *event )
00344 {
00345   mGeneral->writeEvent( event );
00346   mDetails->writeEvent( event );
00347   mAttachments->writeIncidence( event );
00348 
00349   if ( event->organizer() == KOPrefs::instance()->email() ) {
00350     Event *ev = new Event( *event );
00351     ev->registerObserver( 0 );
00352     mDetails->cancelAttendeeEvent( ev );
00353     if ( ev->attendeeCount() > 0 ) {
00354       emit deleteAttendee( ev );
00355     }
00356     delete( ev );
00357   }
00358 
00359   mRecurrence->writeEvent( event );
00360 }
00361 
00362 bool KOEventEditor::validateInput()
00363 {
00364   if ( !mGeneral->validateInput() ) return false;
00365   if ( !mDetails->validateInput() ) return false;
00366   if ( !mRecurrence->validateInput() ) return false;
00367 
00368   return true;
00369 }
00370 
00371 int KOEventEditor::msgItemDelete()
00372 {
00373   return KMessageBox::warningContinueCancel(this,
00374       i18n("This item will be permanently deleted."),
00375       i18n("KOrganizer Confirmation"),i18n("Delete"));
00376 }
00377 
00378 void KOEventEditor::slotLoadTemplate()
00379 {
00380   CalendarLocal cal( KOPrefs::instance()->mTimeZoneId );
00381   Event *event = new Event;
00382   QString templateName = loadTemplate( &cal, event->type(),
00383                                        KOPrefs::instance()->mEventTemplates );
00384   delete event;
00385   if ( templateName.isEmpty() ) {
00386     return;
00387   }
00388 
00389   Event::List events = cal.events();
00390   if ( events.count() == 0 ) {
00391     KMessageBox::error( this,
00392         i18n("Template does not contain a valid event.")
00393         .arg( templateName ) );
00394   } else {
00395     kdDebug(5850) << "KOEventEditor::slotLoadTemplate(): readTemplate" << endl;
00396     readEvent( events.first(), true );
00397   }
00398 }
00399 
00400 void KOEventEditor::saveTemplate( const QString &templateName )
00401 {
00402   Event *event = new Event;
00403   writeEvent( event );
00404   saveAsTemplate( event, templateName );
00405 }
00406 
00407 QObject *KOEventEditor::typeAheadReceiver() const
00408 {
00409   return mGeneral->typeAheadReceiver();
00410 }
00411 
00412 #include "koeventeditor.moc"
KDE Logo
This file is part of the documentation for korganizer Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:38:29 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003