korganizer

kotodoeditor.cpp

00001 /*
00002     This file is part of KOrganizer.
00003 
00004     Copyright (c) 1997, 1998 Preston Brown <pbrown@kde.org>
00005     Copyright (c) 2000-2003 Cornelius Schumacher <schumacher@kde.org>
00006     Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
00007 
00008     This program is free software; you can redistribute it and/or modify
00009     it under the terms of the GNU General Public License as published by
00010     the Free Software Foundation; either version 2 of the License, or
00011     (at your option) any later version.
00012 
00013     This program is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016     GNU General Public License for more details.
00017 
00018     You should have received a copy of the GNU General Public License
00019     along with this program; if not, write to the Free Software
00020     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00021 
00022     As a special exception, permission is given to link this program
00023     with any edition of Qt, and distribute the resulting executable,
00024     without including the source code for Qt in the source distribution.
00025 */
00026 
00027 #include <qtooltip.h>
00028 #include <qframe.h>
00029 #include <qpixmap.h>
00030 #include <qlayout.h>
00031 #include <qdatetime.h>
00032 
00033 #include <kabc/addressee.h>
00034 #include <kiconloader.h>
00035 #include <klocale.h>
00036 #include <kmessagebox.h>
00037 
00038 #include <libkdepim/categoryselectdialog.h>
00039 #include <libkcal/calendarlocal.h>
00040 #include <libkcal/calendarresources.h>
00041 #include <libkcal/resourcecalendar.h>
00042 
00043 #include "koprefs.h"
00044 #include "koeditorattachments.h"
00045 #include "kogroupware.h"
00046 #include "kodialogmanager.h"
00047 #include "incidencechanger.h"
00048 
00049 #include "koeditorgeneraltodo.h"
00050 #include "koeditordetails.h"
00051 #include "koeditorrecurrence.h"
00052 #include "koeditoralarms.h"
00053 
00054 #include "kotodoeditor.h"
00055 #include "kocore.h"
00056 
00057 KOTodoEditor::KOTodoEditor( Calendar *calendar, QWidget *parent ) :
00058   KOIncidenceEditor( QString::null, calendar, parent )
00059 {
00060   mTodo = 0;
00061   mRelatedTodo = 0;
00062 }
00063 
00064 KOTodoEditor::~KOTodoEditor()
00065 {
00066   emit dialogClose( mTodo );
00067 }
00068 
00069 void KOTodoEditor::init()
00070 {
00071   setupGeneral();
00072 //  setupAlarmsTab();
00073   setupRecurrence();
00074   setupAttendeesTab();
00075   setupAttachmentsTab();
00076 
00077   connect( mGeneral, SIGNAL( dateTimeStrChanged( const QString & ) ),
00078            mRecurrence, SLOT( setDateTimeStr( const QString & ) ) );
00079   connect( mGeneral, SIGNAL( signalDateTimeChanged( const QDateTime &, const QDateTime & ) ),
00080            mRecurrence, SLOT( setDateTimes( const QDateTime &, const QDateTime & ) ) );
00081 }
00082 
00083 void KOTodoEditor::reload()
00084 {
00085   if ( mTodo ) readTodo( mTodo );
00086 }
00087 
00088 void KOTodoEditor::setupGeneral()
00089 {
00090   mGeneral = new KOEditorGeneralTodo(this);
00091 
00092   connect(mGeneral,SIGNAL(openCategoryDialog()),mCategoryDialog,SLOT(show()));
00093   connect(mCategoryDialog, SIGNAL(categoriesSelected(const QString &)),
00094           mGeneral,SLOT(setCategories(const QString &)));
00095 
00096   if (KOPrefs::instance()->mCompactDialogs) {
00097     QFrame *topFrame = addPage(i18n("General"));
00098 
00099     QBoxLayout *topLayout = new QVBoxLayout(topFrame);
00100     topLayout->setMargin(marginHint());
00101     topLayout->setSpacing(spacingHint());
00102 
00103     mGeneral->initHeader(topFrame,topLayout);
00104     mGeneral->initTime(topFrame,topLayout);
00105     QHBoxLayout *priorityLayout = new QHBoxLayout( topLayout );
00106     mGeneral->initPriority(topFrame,priorityLayout);
00107     mGeneral->initCategories( topFrame, topLayout );
00108     topLayout->addStretch(1);
00109 
00110     QFrame *topFrame2 = addPage(i18n("Details"));
00111 
00112     QBoxLayout *topLayout2 = new QVBoxLayout(topFrame2);
00113     topLayout2->setMargin(marginHint());
00114     topLayout2->setSpacing(spacingHint());
00115 
00116     QHBoxLayout *completionLayout = new QHBoxLayout( topLayout2 );
00117     mGeneral->initCompletion(topFrame2,completionLayout);
00118 
00119     mGeneral->initAlarm(topFrame,topLayout);
00120     mGeneral->enableAlarm( false );
00121 
00122     mGeneral->initSecrecy( topFrame2, topLayout2 );
00123     mGeneral->initDescription(topFrame2,topLayout2);
00124   } else {
00125     QFrame *topFrame = addPage(i18n("&General"));
00126 
00127     QBoxLayout *topLayout = new QVBoxLayout(topFrame);
00128     topLayout->setSpacing(spacingHint());
00129 
00130     mGeneral->initHeader(topFrame,topLayout);
00131     mGeneral->initTime(topFrame,topLayout);
00132     mGeneral->initStatus(topFrame,topLayout);
00133     QBoxLayout *alarmLineLayout = new QHBoxLayout(topLayout);
00134     mGeneral->initAlarm(topFrame,alarmLineLayout);
00135     alarmLineLayout->addStretch( 1 );
00136     mGeneral->initDescription(topFrame,topLayout);
00137     QBoxLayout *detailsLayout = new QHBoxLayout(topLayout);
00138     mGeneral->initCategories( topFrame, detailsLayout );
00139     mGeneral->initSecrecy( topFrame, detailsLayout );
00140   }
00141 
00142   mGeneral->finishSetup();
00143 }
00144 
00145 void KOTodoEditor::setupRecurrence()
00146 {
00147   QFrame *topFrame = addPage( i18n("Rec&urrence") );
00148 
00149   QBoxLayout *topLayout = new QVBoxLayout( topFrame );
00150 
00151   mRecurrence = new KOEditorRecurrence( topFrame );
00152   topLayout->addWidget( mRecurrence );
00153 
00154   mRecurrence->setEnabled( false );
00155   connect(mGeneral,SIGNAL(dueDateEditToggle( bool ) ),
00156           mRecurrence, SLOT( setEnabled( bool ) ) );
00157 }
00158 
00159 void KOTodoEditor::editIncidence(Incidence *incidence)
00160 {
00161   Todo *todo=dynamic_cast<Todo*>(incidence);
00162   if (todo)
00163   {
00164     init();
00165 
00166     mTodo = todo;
00167     readTodo(mTodo);
00168   }
00169 
00170   setCaption( i18n("Edit To-do") );
00171 }
00172 
00173 void KOTodoEditor::newTodo( const QDateTime &due, Todo *relatedTodo, bool allDay)
00174 {
00175   init();
00176 
00177   mTodo = 0;
00178   setDefaults(due,relatedTodo,allDay);
00179 
00180   setCaption( i18n("New To-do") );
00181 }
00182 
00183 void KOTodoEditor::newTodo( const QString &text )
00184 {
00185   init();
00186 
00187   mTodo = 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   setCaption( i18n("New To-do") );
00202 }
00203 
00204 void KOTodoEditor::newTodo( const QString &summary,
00205                             const QString &description,
00206                             const QString &attachment )
00207 {
00208   init();
00209 
00210   mTodo = 0;
00211 
00212   loadDefaults();
00213 
00214   mGeneral->setSummary( summary );
00215   mGeneral->setDescription( description );
00216 
00217   if ( !attachment.isEmpty() ) {
00218     mAttachments->addAttachment( attachment );
00219   }
00220 
00221   setCaption( i18n("New To-do") );
00222 }
00223 
00224 void KOTodoEditor::newTodo( const QString &summary,
00225                             const QString &description,
00226                             const QString &attachment,
00227                             const QStringList &attendees )
00228 {
00229   newTodo( summary, description, attachment );
00230 
00231   QStringList::ConstIterator it;
00232   for ( it = attendees.begin(); it != attendees.end(); ++it ) {
00233     QString name, email;
00234     KABC::Addressee::parseEmailAddress( *it, name, email );
00235     mDetails->insertAttendee( new Attendee( name, email ) );
00236   }
00237 }
00238 
00239 void KOTodoEditor::loadDefaults()
00240 {
00241   setDefaults(QDateTime::currentDateTime().addDays(7),0,false);
00242 }
00243 
00244 bool KOTodoEditor::processInput()
00245 {
00246   if ( !validateInput() ) return false;
00247 
00248   if ( mTodo ) {
00249     bool rc = true;
00250     Todo *oldTodo = mTodo->clone();
00251     Todo *todo = mTodo->clone();
00252 
00253     kdDebug(5850) << "KOTodoEditor::processInput() write event." << endl;
00254     writeTodo( todo );
00255     kdDebug(5850) << "KOTodoEditor::processInput() event written." << endl;
00256 
00257     if( *mTodo == *todo )
00258       // Don't do anything
00259       kdDebug(5850) << "Todo not changed\n";
00260     else {
00261       kdDebug(5850) << "Todo changed\n";
00262       //IncidenceChanger::assignIncidence( mTodo, todo );
00263       writeTodo( mTodo );
00264       mChanger->changeIncidence( oldTodo, mTodo );
00265     }
00266     delete todo;
00267     delete oldTodo;
00268     return rc;
00269 
00270   } else {
00271     mTodo = new Todo;
00272     mTodo->setOrganizer( Person( KOPrefs::instance()->fullName(),
00273                          KOPrefs::instance()->email() ) );
00274 
00275     writeTodo( mTodo );
00276 
00277     if ( !mChanger->addIncidence( mTodo ) ) {
00278       delete mTodo;
00279       mTodo = 0;
00280       return false;
00281     }
00282   }
00283 
00284   return true;
00285 
00286 }
00287 
00288 void KOTodoEditor::deleteTodo()
00289 {
00290   if (mTodo)
00291     emit deleteIncidenceSignal( mTodo );
00292   emit dialogClose(mTodo);
00293   reject();
00294 }
00295 
00296 void KOTodoEditor::setDefaults( const QDateTime &due, Todo *relatedEvent, bool allDay )
00297 {
00298   mRelatedTodo = relatedEvent;
00299 
00300   // inherit some properties from parent todo
00301   if ( mRelatedTodo ) {
00302     mGeneral->setCategories( mRelatedTodo->categoriesStr() );
00303     mCategoryDialog->setSelected( mRelatedTodo->categories() );
00304     if ( mRelatedTodo->hasDueDate() )
00305       mGeneral->setDefaults( mRelatedTodo->dtDue(), allDay );
00306     else
00307       mGeneral->setDefaults( due, allDay );
00308   }
00309   else
00310     mGeneral->setDefaults( due, allDay );
00311 
00312   mDetails->setDefaults();
00313   if ( mTodo )
00314     mRecurrence->setDefaults( mTodo->dtStart(), due, false );
00315   else
00316     mRecurrence->setDefaults( QDateTime::currentDateTime(), due, false );
00317   mAttachments->setDefaults();
00318 }
00319 
00320 void KOTodoEditor::readTodo( Todo *todo )
00321 {
00322   kdDebug(5850)<<"read todo"<<endl;
00323   mGeneral->readTodo( todo );
00324   mDetails->readEvent( todo );
00325 //  mAlarms->readIncidence( todo );
00326   mRecurrence->readIncidence( todo );
00327   mAttachments->readIncidence( todo );
00328 
00329   // categories
00330   mCategoryDialog->setSelected( todo->categories() );
00331   createEmbeddedURLPages( todo );
00332   readDesignerFields( todo );
00333 }
00334 
00335 void KOTodoEditor::writeTodo( Todo *todo )
00336 {
00337   Incidence *oldIncidence = todo->clone();
00338 
00339   mRecurrence->writeIncidence( todo );
00340   mGeneral->writeTodo( todo );
00341   mDetails->writeEvent( todo );
00342   mAttachments->writeIncidence( todo );
00343 
00344   if ( *(oldIncidence->recurrence()) != *(todo->recurrence() ) ) {
00345     todo->setDtDue( todo->dtDue(), true );
00346     if ( todo->hasStartDate() )
00347       todo->setDtStart( todo->dtStart() );
00348   }
00349   writeDesignerFields( todo );
00350 
00351   // set related event, i.e. parent to-do in this case.
00352   if ( mRelatedTodo ) {
00353     todo->setRelatedTo( mRelatedTodo );
00354   }
00355 
00356   cancelRemovedAttendees( todo );
00357 }
00358 
00359 bool KOTodoEditor::validateInput()
00360 {
00361   if ( !mGeneral->validateInput() ) return false;
00362   if ( !mRecurrence->validateInput() ) return false;
00363   if ( !mDetails->validateInput() ) return false;
00364   return true;
00365 }
00366 
00367 int KOTodoEditor::msgItemDelete()
00368 {
00369   return KMessageBox::warningContinueCancel(this,
00370       i18n("This item will be permanently deleted."),
00371       i18n("KOrganizer Confirmation"), KStdGuiItem::del() );
00372 }
00373 
00374 void KOTodoEditor::modified (int /*modification*/)
00375 {
00376   // Play dump, just reload the todo. This dialog has become so complicated that
00377   // there is no point in trying to be smart here...
00378   reload();
00379 }
00380 
00381 void KOTodoEditor::loadTemplate( /*const*/ CalendarLocal& cal )
00382 {
00383   Todo::List todos = cal.todos();
00384   if ( todos.count() == 0 ) {
00385     KMessageBox::error( this,
00386         i18n("Template does not contain a valid to-do.") );
00387   } else {
00388     readTodo( todos.first() );
00389   }
00390 }
00391 
00392 void KOTodoEditor::slotSaveTemplate( const QString &templateName )
00393 {
00394   Todo *todo = new Todo;
00395   writeTodo( todo );
00396   saveAsTemplate( todo, templateName );
00397 }
00398 
00399 QStringList& KOTodoEditor::templates() const
00400 {
00401   return KOPrefs::instance()->mTodoTemplates;
00402 }
00403 
00404 #include "kotodoeditor.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys