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