korganizer Library API Documentation

kotodoeditor.cpp

00001 /*
00002     This file is part of KOrganizer.
00003 
00004     Copyright (c) 1997, 1998 Preston Brown
00005     Copyright (c) 2000-2003 Cornelius Schumacher <schumacher@kde.org>
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 <qdatetime.h>
00031 
00032 #include <kabc/addressee.h>
00033 #include <kiconloader.h>
00034 #include <klocale.h>
00035 #include <kmessagebox.h>
00036 
00037 #include <libkdepim/categoryselectdialog.h>
00038 #include <libkcal/calendarlocal.h>
00039 #include <libkcal/calendarresources.h>
00040 #include <libkcal/resourcecalendar.h>
00041 
00042 #include "koprefs.h"
00043 #include "koeditorattachments.h"
00044 #include "kogroupware.h"
00045 #include "kodialogmanager.h"
00046 
00047 #include "koeditorgeneraltodo.h"
00048 #include "koeditordetails.h"
00049 #include "koeditorrecurrence.h"
00050 
00051 #include "kotodoeditor.h"
00052 #include "kocore.h"
00053 
00054 KOTodoEditor::KOTodoEditor( Calendar *calendar, QWidget *parent ) :
00055   KOIncidenceEditor( i18n("Edit To-Do"), calendar, parent )
00056 {
00057   mTodo = 0;
00058   mRelatedTodo = 0;
00059 }
00060 
00061 KOTodoEditor::~KOTodoEditor()
00062 {
00063   emit dialogClose( mTodo );
00064 }
00065 
00066 void KOTodoEditor::init()
00067 {
00068   setupGeneral();
00069   setupAttendeesTab();
00070   setupRecurrence();
00071   setupAttachmentsTab();
00072 
00073   connect( mGeneral, SIGNAL( dateTimeStrChanged( const QString & ) ),
00074            mRecurrence, SLOT( setDateTimeStr( const QString & ) ) );
00075   connect( mGeneral, SIGNAL( signalDateTimeChanged( QDateTime, QDateTime ) ),
00076            mRecurrence, SLOT( setDateTimes( QDateTime, QDateTime ) ) );
00077 }
00078 
00079 void KOTodoEditor::reload()
00080 {
00081 kdDebug()<<"reloading todo"<<endl;
00082   if ( mTodo ) readTodo( mTodo );
00083 }
00084 
00085 void KOTodoEditor::setupGeneral()
00086 {
00087   mGeneral = new KOEditorGeneralTodo(this);
00088 
00089   connect(mGeneral,SIGNAL(openCategoryDialog()),mCategoryDialog,SLOT(show()));
00090   connect(mCategoryDialog, SIGNAL(categoriesSelected(const QString &)),
00091           mGeneral,SLOT(setCategories(const QString &)));
00092   connect(mGeneral,SIGNAL( todoCompleted( Todo * )),
00093                    SIGNAL( todoCompleted( Todo * ) ) );
00094 
00095   if (KOPrefs::instance()->mCompactDialogs) {
00096     QFrame *topFrame = addPage(i18n("General"));
00097 
00098     QBoxLayout *topLayout = new QVBoxLayout(topFrame);
00099     topLayout->setMargin(marginHint());
00100     topLayout->setSpacing(spacingHint());
00101 
00102     mGeneral->initHeader(topFrame,topLayout);
00103     mGeneral->initTime(topFrame,topLayout);
00104     QHBoxLayout *priorityLayout = new QHBoxLayout( topLayout );
00105     mGeneral->initPriority(topFrame,priorityLayout);
00106     mGeneral->initCategories( topFrame, topLayout );
00107     topLayout->addStretch(1);
00108 
00109     QFrame *topFrame2 = addPage(i18n("Details"));
00110 
00111     QBoxLayout *topLayout2 = new QVBoxLayout(topFrame2);
00112     topLayout2->setMargin(marginHint());
00113     topLayout2->setSpacing(spacingHint());
00114 
00115     QHBoxLayout *completionLayout = new QHBoxLayout( topLayout2 );
00116     mGeneral->initCompletion(topFrame2,completionLayout);
00117 
00118     mGeneral->initAlarm(topFrame,topLayout);
00119     mGeneral->enableAlarm( false );
00120 
00121     mGeneral->initSecrecy( topFrame2, topLayout2 );
00122     mGeneral->initDescription(topFrame2,topLayout2);
00123   } else {
00124     QFrame *topFrame = addPage(i18n("&General"));
00125 
00126     QBoxLayout *topLayout = new QVBoxLayout(topFrame);
00127     topLayout->setSpacing(spacingHint());
00128 
00129     mGeneral->initHeader(topFrame,topLayout);
00130     mGeneral->initTime(topFrame,topLayout);
00131     mGeneral->initStatus(topFrame,topLayout);
00132     QBoxLayout *alarmLineLayout = new QHBoxLayout(topLayout);
00133     mGeneral->initAlarm(topFrame,alarmLineLayout);
00134     mGeneral->initDescription(topFrame,topLayout);
00135     QBoxLayout *detailsLayout = new QHBoxLayout(topLayout);
00136     mGeneral->initCategories( topFrame, detailsLayout );
00137     mGeneral->initSecrecy( topFrame, detailsLayout );
00138   }
00139 
00140   mGeneral->finishSetup();
00141 }
00142 
00143 void KOTodoEditor::setupRecurrence()
00144 {
00145   QFrame *topFrame = addPage( i18n("Rec&urrence") );
00146 
00147   QBoxLayout *topLayout = new QVBoxLayout( topFrame );
00148 
00149   mRecurrence = new KOEditorRecurrence( topFrame );
00150   topLayout->addWidget( mRecurrence );
00151 
00152   mRecurrence->setEnabled( false );
00153   connect(mGeneral,SIGNAL(dueDateEditToggle( bool ) ),
00154           mRecurrence, SLOT( setEnabled( bool ) ) );
00155 }
00156 
00157 void KOTodoEditor::editIncidence(Incidence *incidence)
00158 {
00159   Todo *todo=dynamic_cast<Todo*>(incidence);
00160   if (todo)
00161   {
00162     init();
00163 
00164     mTodo = todo;
00165     readTodo(mTodo);
00166   }
00167 }
00168 
00169 void KOTodoEditor::newTodo(QDateTime due,Todo *relatedTodo,bool allDay)
00170 {
00171   init();
00172 
00173   mTodo = 0;
00174   setDefaults(due,relatedTodo,allDay);
00175 }
00176 
00177 void KOTodoEditor::newTodo( const QString &text )
00178 {
00179   init();
00180 
00181   mTodo = 0;
00182 
00183   loadDefaults();
00184 
00185   mGeneral->setDescription( text );
00186 
00187   int pos = text.find( "\n" );
00188   if ( pos > 0 ) {
00189     mGeneral->setSummary( text.left( pos ) );
00190     mGeneral->setDescription( text );
00191   } else {
00192     mGeneral->setSummary( text );
00193   }
00194 }
00195 
00196 void KOTodoEditor::newTodo( const QString &summary,
00197                             const QString &description,
00198                             const QString &attachment )
00199 {
00200   init();
00201 
00202   mTodo = 0;
00203 
00204   loadDefaults();
00205 
00206   mGeneral->setSummary( summary );
00207   mGeneral->setDescription( description );
00208 
00209   if ( !attachment.isEmpty() ) {
00210     mAttachments->addAttachment( attachment );
00211   }
00212 }
00213 
00214 void KOTodoEditor::newTodo( const QString &summary,
00215                             const QString &description,
00216                             const QString &attachment,
00217                             const QStringList &attendees )
00218 {
00219   newTodo( summary, description, attachment );
00220 
00221   QStringList::ConstIterator it;
00222   for ( it = attendees.begin(); it != attendees.end(); ++it ) {
00223     QString name, email;
00224     KABC::Addressee::parseEmailAddress( *it, name, email );
00225     mDetails->insertAttendee( new Attendee( name, email ) );
00226   }
00227 }
00228 
00229 void KOTodoEditor::loadDefaults()
00230 {
00231   setDefaults(QDateTime::currentDateTime().addDays(7),0,false);
00232 }
00233 
00234 // TODO_RK: make sure calendar()->endChange is called somewhere!
00235 bool KOTodoEditor::processInput()
00236 {
00237   if ( !validateInput() ) return false;
00238 
00239   if ( mTodo ) {
00240     bool rc = true;
00241     Todo *todo = mTodo->clone();
00242     Todo *oldTodo = mTodo->clone();
00243 
00244     kdDebug(5850) << "KOTodoEditor::processInput() write todo." << endl;
00245     writeTodo( todo );
00246     kdDebug(5850) << "KOTodoEditor::processInput() event written." << endl;
00247 
00248     if( *mTodo == *todo )
00249       // Don't do anything
00250       kdDebug(5850) << "Todo not changed\n";
00251     else {
00252       kdDebug(5850) << "Todo changed\n";
00253       int revision = todo->revision();
00254       todo->setRevision( revision + 1 );
00255       if( !KOPrefs::instance()->mUseGroupwareCommunication ||
00256           KOGroupware::instance()->sendICalMessage( this,
00257                                                     KCal::Scheduler::Request,
00258                                                     todo ) ) {
00259         // Accept the event changes
00260         writeTodo( mTodo );
00261         mTodo->setRevision( revision + 1 );
00262         emit incidenceChanged( oldTodo, mTodo );
00263       } else {
00264         // Revert the changes
00265         todo->setRevision( revision );
00266         rc = false;
00267       }
00268     }
00269     delete todo;
00270     delete oldTodo;
00271     return rc;
00272 
00273   } else {
00274     mTodo = new Todo;
00275     mTodo->setOrganizer( Person( KOPrefs::instance()->fullName(), 
00276                          KOPrefs::instance()->email() ) );
00277 
00278     writeTodo( mTodo );
00279     if ( KOPrefs::instance()->mUseGroupwareCommunication ) {
00280       if ( !KOGroupware::instance()->sendICalMessage( this,
00281                                                       KCal::Scheduler::Request,
00282                                                       mTodo ) ) {
00283         kdError() << "sendIcalMessage failed." << endl;
00284       }
00285     }
00286 
00287     if ( !mCalendar->addIncidence( mTodo ) ) {
00288       KODialogManager::errorSaveTodo( this );
00289       delete mTodo;
00290       mTodo = 0;
00291       return false;
00292     }
00293 
00294     emit incidenceAdded( mTodo );
00295 
00296   }
00297 
00298   return true;
00299 
00300 }
00301 
00302 void KOTodoEditor::processCancel()
00303 {
00304   if ( mTodo ) {
00305     emit editCanceled( mTodo );
00306   }
00307 }
00308 
00309 void KOTodoEditor::deleteTodo()
00310 {
00311   if (mTodo) {
00312     if (KOPrefs::instance()->mConfirm) {
00313       switch (msgItemDelete()) {
00314         case KMessageBox::Continue: // OK
00315           emit incidenceToBeDeleted(mTodo);
00316           emit dialogClose(mTodo);
00317           mCalendar->deleteTodo(mTodo);
00318           emit incidenceDeleted( mTodo );
00319           reject();
00320           break;
00321       }
00322     }
00323     else {
00324       emit incidenceToBeDeleted(mTodo);
00325       emit dialogClose(mTodo);
00326       mCalendar->deleteTodo(mTodo);
00327       emit incidenceDeleted( mTodo );
00328       reject();
00329     }
00330   } else {
00331     reject();
00332   }
00333 }
00334 
00335 void KOTodoEditor::setDefaults( QDateTime due, Todo *relatedEvent, bool allDay )
00336 {
00337   mRelatedTodo = relatedEvent;
00338 
00339   // inherit some properties from parent todo
00340   if ( mRelatedTodo ) {
00341     mGeneral->setCategories( mRelatedTodo->categoriesStr() );
00342     mCategoryDialog->setSelected( mRelatedTodo->categories() );
00343     if ( mRelatedTodo->hasDueDate() )
00344       mGeneral->setDefaults( mRelatedTodo->dtDue(), allDay );
00345     else
00346       mGeneral->setDefaults( due, allDay );
00347   }
00348   else
00349     mGeneral->setDefaults( due, allDay );
00350 
00351   mDetails->setDefaults();
00352   if ( mTodo )
00353     mRecurrence->setDefaults( mTodo->dtStart(), due, false );
00354   else
00355     mRecurrence->setDefaults( QDateTime::currentDateTime(), due, false );
00356   mAttachments->setDefaults();
00357 }
00358 
00359 void KOTodoEditor::readTodo( Todo *todo )
00360 {
00361   kdDebug()<<"read todo"<<endl;
00362   mGeneral->readTodo( todo );
00363   mDetails->readEvent( todo );
00364   mRecurrence->readIncidence( todo );
00365   mAttachments->readIncidence( todo );
00366 
00367   // categories
00368   mCategoryDialog->setSelected( todo->categories() );
00369 }
00370 
00371 void KOTodoEditor::writeTodo( Todo *todo )
00372 {
00373   Incidence *oldIncidence = todo->clone();
00374 
00375   mRecurrence->writeIncidence( todo );
00376   mGeneral->writeTodo( todo );
00377   mDetails->writeEvent( todo );
00378   mAttachments->writeIncidence( todo );
00379 
00380   if ( *(oldIncidence->recurrence()) != *(todo->recurrence() ) ) {
00381     todo->setDtDue( todo->dtDue(), true );
00382     if ( todo->hasStartDate() )
00383       todo->setDtStart( todo->dtStart() );
00384   }
00385 
00386   // set related event, i.e. parent to-do in this case.
00387   if ( mRelatedTodo ) {
00388     todo->setRelatedTo( mRelatedTodo );
00389   }
00390 
00391   cancelRemovedAttendees( todo );
00392 }
00393 
00394 bool KOTodoEditor::validateInput()
00395 {
00396   if ( !mGeneral->validateInput() ) return false;
00397   if ( !mRecurrence->validateInput() ) return false;
00398   if ( !mDetails->validateInput() ) return false;
00399   return true;
00400 }
00401 
00402 int KOTodoEditor::msgItemDelete()
00403 {
00404   return KMessageBox::warningContinueCancel(this,
00405       i18n("This item will be permanently deleted."),
00406       i18n("KOrganizer Confirmation"),KGuiItem(i18n("Delete"),"editdelete"));
00407 }
00408 
00409 void KOTodoEditor::modified (int /*modification*/)
00410 {
00411   // Play dump, just reload the todo. This dialog has become so complicated that
00412   // there is no point in trying to be smart here...
00413   reload();
00414 }
00415 
00416 void KOTodoEditor::loadTemplate( /*const*/ CalendarLocal& cal )
00417 {
00418   Todo::List todos = cal.todos();
00419   if ( todos.count() == 0 ) {
00420     KMessageBox::error( this,
00421         i18n("Template does not contain a valid to-do.") );
00422   } else {
00423     readTodo( todos.first() );
00424   }
00425 }
00426 
00427 void KOTodoEditor::slotSaveTemplate( const QString &templateName )
00428 {
00429   Todo *todo = new Todo;
00430   writeTodo( todo );
00431   saveAsTemplate( todo, templateName );
00432 }
00433 
00434 QStringList& KOTodoEditor::templates() const
00435 {
00436   return KOPrefs::instance()->mTodoTemplates;
00437 }
00438 
00439 #include "kotodoeditor.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 Thu May 3 20:24:57 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003