korganizer

koeventeditor.cpp

00001 /*
00002     This file is part of KOrganizer.
00003 
00004     Copyright (c) 2001, 2002, 2003 Cornelius Schumacher <schumacher@kde.org>
00005     Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
00006 
00007     This program is free software; you can redistribute it and/or modify
00008     it under the terms of the GNU General Public License as published by
00009     the Free Software Foundation; either version 2 of the License, or
00010     (at your option) any later version.
00011 
00012     This program is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00015     GNU General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program; if not, write to the Free Software
00019     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020 
00021     As a special exception, permission is given to link this program
00022     with any edition of Qt, and distribute the resulting executable,
00023     without including the source code for Qt in the source distribution.
00024 */
00025 
00026 #include <qtooltip.h>
00027 #include <qframe.h>
00028 #include <qpixmap.h>
00029 #include <qlayout.h>
00030 #include <qwidgetstack.h>
00031 #include <qwhatsthis.h>
00032 
00033 #include <kabc/addressee.h>
00034 #include <kiconloader.h>
00035 #include <kdebug.h>
00036 #include <klocale.h>
00037 #include <kmessagebox.h>
00038 #include <libkcal/calendarresources.h>
00039 #include <libkcal/resourcecalendar.h>
00040 
00041 #include <libkdepim/categoryselectdialog.h>
00042 #include <libkcal/calendarlocal.h>
00043 
00044 #include "koprefs.h"
00045 #include "koeditorgeneralevent.h"
00046 #include "koeditoralarms.h"
00047 #include "koeditorrecurrence.h"
00048 #include "koeditordetails.h"
00049 #include "koeditorattachments.h"
00050 #include "koeditorfreebusy.h"
00051 #include "kogroupware.h"
00052 #include "kodialogmanager.h"
00053 #include "incidencechanger.h"
00054 
00055 #include "koeventeditor.h"
00056 
00057 KOEventEditor::KOEventEditor( Calendar *calendar, QWidget *parent )
00058   : KOIncidenceEditor( QString::null, calendar, parent ),
00059     mEvent( 0 )
00060 {
00061 }
00062 
00063 KOEventEditor::~KOEventEditor()
00064 {
00065   emit dialogClose( mEvent );
00066 }
00067 
00068 void KOEventEditor::init()
00069 {
00070   setupGeneral();
00071 //  setupAlarmsTab();
00072   setupRecurrence();
00073   setupAttendeesTab();
00074   setupFreeBusy();
00075   setupAttachmentsTab();
00076   setupDesignerTabs( "event" );
00077 
00078   mDetails->setFreeBusyWidget( mFreeBusy );
00079 
00080   // Propagate date time settings to recurrence tab
00081   connect( mGeneral, SIGNAL( dateTimesChanged( const QDateTime &, const QDateTime & ) ),
00082            mRecurrence, SLOT( setDateTimes( const QDateTime &, const QDateTime &) ) );
00083   connect( mGeneral, SIGNAL( dateTimeStrChanged( const QString & ) ),
00084            mRecurrence, SLOT( setDateTimeStr( const QString & ) ) );
00085   connect( mFreeBusy, SIGNAL( dateTimesChanged( const QDateTime &, const QDateTime & ) ),
00086            mRecurrence, SLOT( setDateTimes( const QDateTime &, const QDateTime & ) ) );
00087 
00088   // Propagate date time settings to gantt tab and back
00089   connect( mGeneral, SIGNAL( dateTimesChanged( const QDateTime &, const QDateTime & ) ),
00090            mFreeBusy, SLOT( slotUpdateGanttView( const QDateTime &, const QDateTime & ) ) );
00091   connect( mFreeBusy, SIGNAL( dateTimesChanged( const QDateTime &, const QDateTime & ) ),
00092            mGeneral, SLOT( setDateTimes( const QDateTime &, const QDateTime & ) ) );
00093 
00094   // Category dialog
00095   connect( mGeneral, SIGNAL( openCategoryDialog() ),
00096            mCategoryDialog, SLOT( show() ) );
00097   connect( mCategoryDialog, SIGNAL( categoriesSelected( const QString & ) ),
00098            mGeneral, SLOT( setCategories( const QString & ) ) );
00099 
00100   connect( mGeneral, SIGNAL( focusReceivedSignal() ),
00101            SIGNAL( focusReceivedSignal() ) );
00102 }
00103 
00104 void KOEventEditor::reload()
00105 {
00106   kdDebug(5850) << "KOEventEditor::reload()" << endl;
00107 
00108   if ( mEvent ) readEvent( mEvent );
00109 }
00110 
00111 void KOEventEditor::setupGeneral()
00112 {
00113   mGeneral = new KOEditorGeneralEvent( this );
00114 
00115   if( KOPrefs::instance()->mCompactDialogs ) {
00116     QFrame *topFrame = addPage(i18n("General"));
00117     QWhatsThis::add( topFrame,
00118                      i18n("The General tab allows you to set the most common "
00119                           "options for the event.") );
00120 
00121     QBoxLayout *topLayout = new QVBoxLayout(topFrame);
00122     topLayout->setSpacing(spacingHint());
00123 
00124     mGeneral->initHeader(topFrame,topLayout);
00125     mGeneral->initTime(topFrame,topLayout);
00126 //    QBoxLayout *alarmLineLayout = new QHBoxLayout(topLayout);
00127     mGeneral->initAlarm(topFrame,topLayout);
00128     mGeneral->enableAlarm( false );
00129     mGeneral->initCategories( topFrame, topLayout );
00130 
00131     topLayout->addStretch( 1 );
00132 
00133     QFrame *topFrame2 = addPage(i18n("Details"));
00134 
00135     QBoxLayout *topLayout2 = new QVBoxLayout(topFrame2);
00136     topLayout2->setSpacing(spacingHint());
00137 
00138     mGeneral->initClass(topFrame2,topLayout2);
00139     mGeneral->initSecrecy( topFrame2, topLayout2 );
00140     mGeneral->initDescription(topFrame2,topLayout2);
00141   } else {
00142     QFrame *topFrame = addPage(i18n("&General"));
00143     QWhatsThis::add( topFrame,
00144                      i18n("The General tab allows you to set the most common "
00145                           "options for the event.") );
00146 
00147     QBoxLayout *topLayout = new QVBoxLayout(topFrame);
00148     topLayout->setSpacing(spacingHint());
00149 
00150     mGeneral->initHeader(topFrame,topLayout);
00151     mGeneral->initTime(topFrame,topLayout);
00152     QBoxLayout *alarmLineLayout = new QHBoxLayout(topLayout);
00153     mGeneral->initAlarm(topFrame,alarmLineLayout);
00154     alarmLineLayout->addStretch( 1 );
00155     mGeneral->initClass(topFrame,alarmLineLayout);
00156     mGeneral->initDescription(topFrame,topLayout);
00157     QBoxLayout *detailsLayout = new QHBoxLayout(topLayout);
00158     mGeneral->initCategories( topFrame, detailsLayout );
00159     mGeneral->initSecrecy( topFrame, detailsLayout );
00160   }
00161 
00162   mGeneral->finishSetup();
00163 }
00164 
00165 void KOEventEditor::modified (int /*modification*/)
00166 {
00167   // Play dump, just reload the event. This dialog has become so complicated
00168   // that there is no point in trying to be smart here...
00169   reload();
00170 }
00171 
00172 void KOEventEditor::setupRecurrence()
00173 {
00174   QFrame *topFrame = addPage( i18n("Rec&urrence") );
00175 
00176   QWhatsThis::add( topFrame,
00177         i18n("The Recurrence tab allows you to set options on "
00178        "how often this event recurs.") );
00179 
00180   QBoxLayout *topLayout = new QVBoxLayout( topFrame );
00181 
00182   mRecurrence = new KOEditorRecurrence( topFrame );
00183   topLayout->addWidget( mRecurrence );
00184 }
00185 
00186 void KOEventEditor::setupFreeBusy()
00187 {
00188   QFrame *freeBusyPage = addPage( i18n("&Free/Busy") );
00189   QWhatsThis::add( freeBusyPage,
00190         i18n("The Free/Busy tab allows you to see whether "
00191        "other attendees are free or busy during your event.") );
00192 
00193   QBoxLayout *topLayout = new QVBoxLayout( freeBusyPage );
00194 
00195   mFreeBusy = new KOEditorFreeBusy( spacingHint(), freeBusyPage );
00196   topLayout->addWidget( mFreeBusy );
00197 }
00198 
00199 void KOEventEditor::editIncidence( Incidence *incidence )
00200 {
00201   Event*event = dynamic_cast<Event*>(incidence);
00202   if ( event ) {
00203     init();
00204 
00205     mEvent = event;
00206     readEvent(mEvent);
00207   }
00208 
00209   setCaption( i18n("Edit Event") );
00210 }
00211 
00212 void KOEventEditor::newEvent( const QDateTime &from, const QDateTime &to,
00213                               bool allDay )
00214 {
00215   init();
00216 
00217   mEvent = 0;
00218   setDefaults(from,to,allDay);
00219 
00220   setCaption( i18n("New Event") );
00221 }
00222 
00223 void KOEventEditor::newEvent( const QString &text )
00224 {
00225   init();
00226 
00227   mEvent = 0;
00228 
00229   loadDefaults();
00230 
00231   mGeneral->setDescription( text );
00232 
00233   int pos = text.find( "\n" );
00234   if ( pos > 0 ) {
00235     mGeneral->setSummary( text.left( pos ) );
00236     mGeneral->setDescription( text );
00237   } else {
00238     mGeneral->setSummary( text );
00239   }
00240 
00241   setCaption( i18n("New Event") );
00242 }
00243 
00244 void KOEventEditor::newEvent( const QString &summary,
00245                               const QString &description,
00246                               const QString &attachment )
00247 {
00248   init();
00249 
00250   mEvent = 0;
00251 
00252   loadDefaults();
00253 
00254   mGeneral->setSummary( summary );
00255   mGeneral->setDescription( description );
00256 
00257   if ( !attachment.isEmpty() ) {
00258     mAttachments->addAttachment( attachment );
00259   }
00260 
00261   setCaption( i18n("New Event") );
00262 }
00263 
00264 void KOEventEditor::newEvent( const QString &summary,
00265                               const QString &description,
00266                               const QString &attachment,
00267                               const QStringList &attendees )
00268 {
00269   newEvent( summary, description, attachment );
00270 
00271   QStringList::ConstIterator it;
00272   for ( it = attendees.begin(); it != attendees.end(); ++it ) {
00273     QString name, email;
00274     KABC::Addressee::parseEmailAddress( *it, name, email );
00275     mDetails->insertAttendee( new Attendee( name, email ) );
00276   }
00277 }
00278 
00279 void KOEventEditor::loadDefaults()
00280 {
00281   QDateTime from( QDate::currentDate(), KOPrefs::instance()->mStartTime.time() );
00282   int addSecs = ( KOPrefs::instance()->mDefaultDuration.time().hour()*3600 ) +
00283                 ( KOPrefs::instance()->mDefaultDuration.time().minute()*60 );
00284   QDateTime to( from.addSecs( addSecs ) );
00285 
00286   setDefaults( from, to, false );
00287 }
00288 
00289 bool KOEventEditor::processInput()
00290 {
00291   kdDebug(5850) << "KOEventEditor::processInput()" << endl;
00292 
00293   if ( !validateInput() || !mChanger ) return false;
00294 
00295   if ( mEvent ) {
00296     bool rc = true;
00297     Event *oldEvent = mEvent->clone();
00298     Event *event = mEvent->clone();
00299 
00300     kdDebug(5850) << "KOEventEditor::processInput() write event." << endl;
00301     writeEvent( event );
00302     kdDebug(5850) << "KOEventEditor::processInput() event written." << endl;
00303 
00304     if( *event == *mEvent )
00305       // Don't do anything
00306       kdDebug(5850) << "Event not changed\n";
00307     else {
00308       kdDebug(5850) << "Event changed\n";
00309       //IncidenceChanger::assignIncidence( mEvent, event );
00310       writeEvent( mEvent );
00311       mChanger->changeIncidence( oldEvent, mEvent );
00312     }
00313     delete event;
00314     delete oldEvent;
00315     return rc;
00316   } else {
00317     mEvent = new Event;
00318     mEvent->setOrganizer( Person( KOPrefs::instance()->fullName(),
00319                           KOPrefs::instance()->email() ) );
00320     writeEvent( mEvent );
00321     if ( !mChanger->addIncidence( mEvent ) ) {
00322       delete mEvent;
00323       mEvent = 0;
00324       return false;
00325     }
00326   }
00327 
00328   if ( mFreeBusy ) mFreeBusy->cancelReload();
00329 
00330   return true;
00331 }
00332 
00333 void KOEventEditor::processCancel()
00334 {
00335   kdDebug(5850) << "KOEventEditor::processCancel()" << endl;
00336 
00337   if ( mFreeBusy ) mFreeBusy->cancelReload();
00338 }
00339 
00340 void KOEventEditor::deleteEvent()
00341 {
00342   kdDebug(5850) << "Delete event" << endl;
00343 
00344   if ( mEvent )
00345     emit deleteIncidenceSignal( mEvent );
00346   emit dialogClose( mEvent );
00347   reject();
00348 }
00349 
00350 void KOEventEditor::setDefaults( const QDateTime &from, const QDateTime &to, bool allDay )
00351 {
00352   mGeneral->setDefaults( from, to, allDay );
00353   mDetails->setDefaults();
00354   mAttachments->setDefaults();
00355   mRecurrence->setDefaults( from, to, allDay );
00356   if( mFreeBusy ) {
00357     if ( allDay )
00358       mFreeBusy->setDateTimes( from, to.addDays( 1 ) );
00359     else
00360       mFreeBusy->setDateTimes( from, to );
00361    }
00362 }
00363 
00364 void KOEventEditor::readEvent( Event *event, bool tmpl )
00365 {
00366   mGeneral->readEvent( event, tmpl );
00367   mDetails->readEvent( event );
00368   mRecurrence->readIncidence( event );
00369   mAttachments->readIncidence( event );
00370 //  mAlarms->readIncidence( event );
00371   if ( mFreeBusy ) {
00372     mFreeBusy->readEvent( event );
00373     mFreeBusy->triggerReload();
00374   }
00375 
00376   // categories
00377   mCategoryDialog->setSelected( event->categories() );
00378 
00379   createEmbeddedURLPages( event );
00380   readDesignerFields( event );
00381 }
00382 
00383 void KOEventEditor::writeEvent( Event *event )
00384 {
00385   mGeneral->writeEvent( event );
00386   mDetails->writeEvent( event );
00387   mAttachments->writeIncidence( event );
00388 
00389   cancelRemovedAttendees( event );
00390 
00391   mRecurrence->writeIncidence( event );
00392 
00393   writeDesignerFields( event );
00394 }
00395 
00396 bool KOEventEditor::validateInput()
00397 {
00398   if ( !mGeneral->validateInput() ) return false;
00399   if ( !mDetails->validateInput() ) return false;
00400   if ( !mRecurrence->validateInput() ) return false;
00401 
00402   return true;
00403 }
00404 
00405 int KOEventEditor::msgItemDelete()
00406 {
00407   return KMessageBox::warningContinueCancel(this,
00408       i18n("This item will be permanently deleted."),
00409       i18n("KOrganizer Confirmation"),KGuiItem(i18n("Delete"),"editdelete"));
00410 }
00411 
00412 void KOEventEditor::loadTemplate( /*const*/ CalendarLocal& cal )
00413 {
00414   const Event::List events = cal.events();
00415   if ( events.count() == 0 ) {
00416     KMessageBox::error( this,
00417         i18n("Template does not contain a valid event.") );
00418   } else {
00419     kdDebug(5850) << "KOEventEditor::slotLoadTemplate(): readTemplate" << endl;
00420     readEvent( events.first(), true );
00421   }
00422 }
00423 
00424 QStringList& KOEventEditor::templates() const
00425 {
00426   return KOPrefs::instance()->mEventTemplates;
00427 }
00428 
00429 void KOEventEditor::slotSaveTemplate( const QString &templateName )
00430 {
00431   kdDebug(5006) << "SlotSaveTemplate" << endl;
00432   Event *event = new Event;
00433   writeEvent( event );
00434   saveAsTemplate( event, templateName );
00435 }
00436 
00437 QObject *KOEventEditor::typeAheadReceiver() const
00438 {
00439   return mGeneral->typeAheadReceiver();
00440 }
00441 
00442 #include "koeventeditor.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys