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 <kabc/addressee.h>
00032 #include <kiconloader.h>
00033 #include <kdebug.h>
00034 #include <klocale.h>
00035 #include <kmessagebox.h>
00036 #include <libkcal/calendarresources.h>
00037 #include <libkcal/resourcecalendar.h>
00038 
00039 #include <libkdepim/categoryselectdialog.h>
00040 #include <libkcal/calendarlocal.h>
00041 
00042 #include "koprefs.h"
00043 #include "koeditorgeneralevent.h"
00044 #include "koeditorrecurrence.h"
00045 #include "koeditordetails.h"
00046 #include "koeditorattachments.h"
00047 #include "koeditorfreebusy.h"
00048 #include "kogroupware.h"
00049 #include "kodialogmanager.h"
00050 
00051 #include "koeventeditor.h"
00052 
00053 KOEventEditor::KOEventEditor( Calendar *calendar, QWidget *parent )
00054   : KOIncidenceEditor( i18n("Edit Event"), calendar, parent ),
00055     mEvent( 0 )
00056 {
00057 }
00058 
00059 KOEventEditor::~KOEventEditor()
00060 {
00061   emit dialogClose( mEvent );
00062 }
00063 
00064 void KOEventEditor::init()
00065 {
00066   setupGeneral();
00067   setupAttendeesTab();
00068   setupRecurrence();
00069   setupAttachmentsTab();
00070   setupFreeBusy();
00071   mDetails->setFreeBusyWidget( mFreeBusy );
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   connect( mFreeBusy, SIGNAL( dateTimesChanged( QDateTime, QDateTime ) ),
00079            mRecurrence, SLOT( setDateTimes( QDateTime, QDateTime ) ) );
00080 
00081   // Propagate date time settings to gantt tab and back
00082   connect( mGeneral, SIGNAL( dateTimesChanged( QDateTime, QDateTime ) ),
00083            mFreeBusy, SLOT( slotUpdateGanttView( QDateTime, QDateTime  ) ) );
00084   connect( mFreeBusy, SIGNAL( dateTimesChanged( QDateTime, QDateTime ) ),
00085            mGeneral, SLOT( setDateTimes( QDateTime, QDateTime ) ) );
00086 
00087   // Category dialog
00088   connect( mGeneral, SIGNAL( openCategoryDialog() ),
00089            mCategoryDialog, SLOT( show() ) );
00090   connect( mCategoryDialog, SIGNAL( categoriesSelected( const QString & ) ),
00091            mGeneral, SLOT( setCategories( const QString & ) ) );
00092 
00093   connect( mGeneral, SIGNAL( focusReceivedSignal() ),
00094            SIGNAL( focusReceivedSignal() ) );
00095 }
00096 
00097 void KOEventEditor::reload()
00098 {
00099   kdDebug() << "KOEventEditor::reload()" << endl;
00100 
00101   if ( mEvent ) readEvent( mEvent );
00102 }
00103 
00104 void KOEventEditor::setupGeneral()
00105 {
00106   mGeneral = new KOEditorGeneralEvent( this );
00107 
00108   if( KOPrefs::instance()->mCompactDialogs ) {
00109     QFrame *topFrame = addPage(i18n("General"));
00110 
00111     QBoxLayout *topLayout = new QVBoxLayout(topFrame);
00112     topLayout->setSpacing(spacingHint());
00113 
00114     mGeneral->initHeader(topFrame,topLayout);
00115     mGeneral->initTime(topFrame,topLayout);
00116 //    QBoxLayout *alarmLineLayout = new QHBoxLayout(topLayout);
00117     mGeneral->initAlarm(topFrame,topLayout);
00118     mGeneral->enableAlarm( false );
00119     mGeneral->initCategories( topFrame, topLayout );
00120 
00121     topLayout->addStretch( 1 );
00122 
00123     QFrame *topFrame2 = addPage(i18n("Details"));
00124 
00125     QBoxLayout *topLayout2 = new QVBoxLayout(topFrame2);
00126     topLayout2->setSpacing(spacingHint());
00127 
00128     mGeneral->initClass(topFrame2,topLayout2);
00129     mGeneral->initSecrecy( topFrame2, topLayout2 );
00130     mGeneral->initDescription(topFrame2,topLayout2);
00131   } else {
00132     QFrame *topFrame = addPage(i18n("&General"));
00133 
00134     QBoxLayout *topLayout = new QVBoxLayout(topFrame);
00135     topLayout->setSpacing(spacingHint());
00136 
00137     mGeneral->initHeader(topFrame,topLayout);
00138     mGeneral->initTime(topFrame,topLayout);
00139     QBoxLayout *alarmLineLayout = new QHBoxLayout(topLayout);
00140     mGeneral->initAlarm(topFrame,alarmLineLayout);
00141     mGeneral->initClass(topFrame,alarmLineLayout);
00142     mGeneral->initDescription(topFrame,topLayout);
00143     QBoxLayout *detailsLayout = new QHBoxLayout(topLayout);
00144     mGeneral->initCategories( topFrame, detailsLayout );
00145     mGeneral->initSecrecy( topFrame, detailsLayout );
00146   }
00147 
00148   mGeneral->finishSetup();
00149 }
00150 
00151 void KOEventEditor::modified (int /*modification*/)
00152 {
00153   // Play dump, just reload the event. This dialog has become so complicated
00154   // that there is no point in trying to be smart here...
00155   reload();
00156 }
00157 
00158 void KOEventEditor::setupRecurrence()
00159 {
00160   QFrame *topFrame = addPage( i18n("Rec&urrence") );
00161 
00162   QBoxLayout *topLayout = new QVBoxLayout( topFrame );
00163 
00164   mRecurrence = new KOEditorRecurrence( topFrame );
00165   topLayout->addWidget( mRecurrence );
00166 }
00167 
00168 void KOEventEditor::setupFreeBusy()
00169 {
00170   QFrame *freeBusyPage = addPage( i18n("&Free/Busy") );
00171 
00172   QBoxLayout *topLayout = new QVBoxLayout( freeBusyPage );
00173 
00174   mFreeBusy = new KOEditorFreeBusy( spacingHint(), freeBusyPage );
00175   topLayout->addWidget( mFreeBusy );
00176 }
00177 
00178 void KOEventEditor::editIncidence(Incidence *incidence)
00179 {
00180   Event*event = dynamic_cast<Event*>(incidence);
00181   if (event) {
00182     init();
00183 
00184     mEvent = event;
00185     readEvent(mEvent);
00186   }
00187 }
00188 
00189 void KOEventEditor::newEvent( QDateTime from, QDateTime to, bool allDay )
00190 {
00191   init();
00192 
00193   mEvent = 0;
00194   setDefaults(from,to,allDay);
00195 }
00196 
00197 void KOEventEditor::newEvent( const QString &text )
00198 {
00199   init();
00200 
00201   mEvent = 0;
00202 
00203   loadDefaults();
00204 
00205   mGeneral->setDescription( text );
00206 
00207   int pos = text.find( "\n" );
00208   if ( pos > 0 ) {
00209     mGeneral->setSummary( text.left( pos ) );
00210     mGeneral->setDescription( text );
00211   } else {
00212     mGeneral->setSummary( text );
00213   }
00214 }
00215 
00216 void KOEventEditor::newEvent( const QString &summary,
00217                               const QString &description,
00218                               const QString &attachment )
00219 {
00220   init();
00221 
00222   mEvent = 0;
00223 
00224   loadDefaults();
00225 
00226   mGeneral->setSummary( summary );
00227   mGeneral->setDescription( description );
00228 
00229   if ( !attachment.isEmpty() ) {
00230     mAttachments->addAttachment( attachment );
00231   }
00232 }
00233 
00234 void KOEventEditor::newEvent( const QString &summary,
00235                               const QString &description,
00236                               const QString &attachment,
00237                               const QStringList &attendees )
00238 {
00239   newEvent( summary, description, attachment );
00240 
00241   QStringList::ConstIterator it;
00242   for ( it = attendees.begin(); it != attendees.end(); ++it ) {
00243     QString name, email;
00244     KABC::Addressee::parseEmailAddress( *it, name, email );
00245     mDetails->insertAttendee( new Attendee( name, email ) );
00246   }
00247 }
00248 
00249 void KOEventEditor::loadDefaults()
00250 {
00251   QTime defaultDuration( KOPrefs::instance()->mDefaultDuration.time() );
00252 
00253   QDateTime from(QDate::currentDate(), KOPrefs::instance()->mStartTime.time() );
00254   QDateTime to( from.addSecs(defaultDuration.hour()*3600 +
00255      defaultDuration.minute()*60 + defaultDuration.second()) );
00256 
00257   setDefaults(from,to,false);
00258 }
00259 
00260 // TODO_RK: make sure calendar()->endChange is called somewhere!
00261 bool KOEventEditor::processInput()
00262 {
00263   kdDebug(5850) << "KOEventEditor::processInput()" << endl;
00264 
00265   if ( !validateInput() ) return false;
00266 
00267   if ( mEvent ) {
00268     bool rc = true;
00269     Event *event = mEvent->clone();
00270     Event *oldEvent = mEvent->clone();
00271     kdDebug(5850) << "KOEventEditor::processInput() write event." << endl;
00272     writeEvent( event );
00273     kdDebug(5850) << "KOEventEditor::processInput() event written." << endl;
00274 
00275     if( *mEvent == *event )
00276       // Don't do anything
00277       kdDebug(5850) << "Event not changed\n";
00278     else {
00279       kdDebug(5850) << "Event changed\n";
00280       int revision = event->revision();
00281       event->setRevision( revision + 1 );
00282       if( !KOPrefs::instance()->mUseGroupwareCommunication ||
00283           KOGroupware::instance()->sendICalMessage( this,
00284                                                     KCal::Scheduler::Request,
00285                                                     event ) ) {
00286         // Accept the event changes
00287         writeEvent( mEvent );
00288         mEvent->setRevision( revision + 1 );
00289         emit incidenceChanged( oldEvent, mEvent );
00290       } else {
00291         // Revert the changes
00292         event->setRevision( revision );
00293         rc = false;
00294       }
00295     }
00296     delete event;
00297     delete oldEvent;
00298     return rc;
00299   } else {
00300     mEvent = new Event;
00301     mEvent->setOrganizer( Person( KOPrefs::instance()->fullName(), 
00302                           KOPrefs::instance()->email() ) );
00303     writeEvent( mEvent );
00304     if ( KOPrefs::instance()->mUseGroupwareCommunication ) {
00305       if ( !KOGroupware::instance()->sendICalMessage( this,
00306                                                       KCal::Scheduler::Request,
00307                                                       mEvent ) ) {
00308         kdError() << "sendIcalMessage failed." << endl;
00309       }
00310     }
00311     if ( mCalendar->addEvent( mEvent ) ) {
00312       emit incidenceAdded( mEvent );
00313     } else {
00314       KODialogManager::errorSaveEvent( this );
00315       delete mEvent;
00316       mEvent = 0;
00317       return false;
00318     }
00319   }
00320 
00321   if ( mFreeBusy ) mFreeBusy->cancelReload();
00322 
00323   return true;
00324 }
00325 
00326 void KOEventEditor::processCancel()
00327 {
00328   kdDebug() << "KOEventEditor::processCancel()" << endl;
00329 
00330   if ( mEvent ) {
00331     emit editCanceled( mEvent );
00332   }
00333 
00334   if ( mFreeBusy ) mFreeBusy->cancelReload();
00335 }
00336 
00337 void KOEventEditor::deleteEvent()
00338 {
00339   kdDebug(5850) << "Delete event" << endl;
00340 
00341   if (mEvent) {
00342     bool groupwareCheck = KOPrefs::instance()->mConfirm &&
00343           (!KOPrefs::instance()->mUseGroupwareCommunication ||
00344            KOPrefs::instance()->thatIsMe( mEvent->organizer().email() ) );
00345     if (!groupwareCheck || (msgItemDelete()==KMessageBox::Continue)) {
00346       // Either no groupware check needed, or OK pressed
00347       emit incidenceToBeDeleted(mEvent);
00348       emit dialogClose(mEvent);
00349       mCalendar->deleteEvent(mEvent);
00350       emit incidenceDeleted(mEvent);
00351       reject();
00352     }
00353   } else {
00354     reject();
00355   }
00356 }
00357 
00358 void KOEventEditor::setDefaults( QDateTime from, QDateTime to, bool allDay )
00359 {
00360   mGeneral->setDefaults( from, to, allDay );
00361   mDetails->setDefaults();
00362   mAttachments->setDefaults();
00363   mRecurrence->setDefaults( from, to, allDay );
00364   if( mFreeBusy ) {
00365     if ( allDay )
00366       mFreeBusy->setDateTimes( from, to.addDays( 1 ) );
00367     else
00368       mFreeBusy->setDateTimes( from, to );
00369    }
00370 }
00371 
00372 void KOEventEditor::readEvent( Event *event, bool tmpl )
00373 {
00374   mGeneral->readEvent( event, tmpl );
00375   mDetails->readEvent( event );
00376   mRecurrence->readIncidence( event );
00377   mAttachments->readIncidence( event );
00378   if( mFreeBusy ) {
00379     mFreeBusy->readEvent( event );
00380     mFreeBusy->triggerReload();
00381   }
00382 
00383   // categories
00384   mCategoryDialog->setSelected( event->categories() );
00385 }
00386 
00387 void KOEventEditor::writeEvent( Event *event )
00388 {
00389   mGeneral->writeEvent( event );
00390   mDetails->writeEvent( event );
00391   mAttachments->writeIncidence( event );
00392 
00393   cancelRemovedAttendees( event );
00394 
00395   mRecurrence->writeIncidence( event );
00396 }
00397 
00398 bool KOEventEditor::validateInput()
00399 {
00400   if ( !mGeneral->validateInput() ) return false;
00401   if ( !mDetails->validateInput() ) return false;
00402   if ( !mRecurrence->validateInput() ) return false;
00403 
00404   return true;
00405 }
00406 
00407 int KOEventEditor::msgItemDelete()
00408 {
00409   return KMessageBox::warningContinueCancel(this,
00410       i18n("This item will be permanently deleted."),
00411       i18n("KOrganizer Confirmation"),KGuiItem(i18n("Delete"),"editdelete"));
00412 }
00413 
00414 void KOEventEditor::slotLoadTemplate()
00415 {
00416   CalendarLocal cal( KOPrefs::instance()->mTimeZoneId );
00417   Event *event = new Event;
00418   QString templateName = loadTemplate( &cal, event->type(),
00419                                        KOPrefs::instance()->mEventTemplates );
00420   delete event;
00421   if ( templateName.isEmpty() ) {
00422     return;
00423   }
00424 
00425   Event::List events = cal.events();
00426   if ( events.count() == 0 ) {
00427     KMessageBox::error( this,
00428         i18n("Template does not contain a valid event.")
00429         .arg( templateName ) );
00430   } else {
00431     kdDebug(5850) << "KOEventEditor::slotLoadTemplate(): readTemplate" << endl;
00432     readEvent( events.first(), true );
00433   }
00434 }
00435 
00436 void KOEventEditor::saveTemplate( const QString &templateName )
00437 {
00438   Event *event = new Event;
00439   writeEvent( event );
00440   saveAsTemplate( event, templateName );
00441 }
00442 
00443 QObject *KOEventEditor::typeAheadReceiver() const
00444 {
00445   return mGeneral->typeAheadReceiver();
00446 }
00447 
00448 #include "koeventeditor.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 Wed Mar 23 22:45:25 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003