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 <kiconloader.h>
00034 #include <klocale.h>
00035 #include <kmessagebox.h>
00036 
00037 #include <libkcal/calendarlocal.h>
00038 #include <libkcal/calendarresources.h>
00039 #include <libkcal/resourcecalendar.h>
00040 
00041 #include "koprefs.h"
00042 #include "koeditorattachments.h"
00043 #include "kogroupware.h"
00044 #include "kodialogmanager.h"
00045 #include "incidencechanger.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( QString::null, calendar, parent ),
00056   mTodo( 0 ), mCalendar( 0 ), mRelatedTodo( 0 ), mGeneral( 0 ), mRecurrence( 0 )
00057 {
00058 }
00059 
00060 KOTodoEditor::~KOTodoEditor()
00061 {
00062   emit dialogClose( mTodo );
00063 }
00064 
00065 void KOTodoEditor::init()
00066 {
00067   setupGeneral();
00068   setupRecurrence();
00069   setupAttendeesTab();
00070 
00071   connect( mGeneral, SIGNAL( dateTimeStrChanged( const QString & ) ),
00072            mRecurrence, SLOT( setDateTimeStr( const QString & ) ) );
00073   connect( mGeneral, SIGNAL( signalDateTimeChanged( const QDateTime &, const QDateTime & ) ),
00074            mRecurrence, SLOT( setDateTimes( const QDateTime &, const QDateTime & ) ) );
00075 
00076   connect( mGeneral, SIGNAL( openCategoryDialog() ),
00077            SIGNAL( editCategories() ) );
00078 
00079   connect( mDetails, SIGNAL(updateAttendeeSummary(int)),
00080            mGeneral, SLOT(updateAttendeeSummary(int)) );
00081 
00082   connect( mGeneral, SIGNAL(editRecurrence()),
00083            mRecurrenceDialog, SLOT(show()) );
00084   connect( mRecurrenceDialog, SIGNAL(okClicked()),
00085            SLOT(updateRecurrenceSummary()) );
00086 }
00087 
00088 void KOTodoEditor::reload()
00089 {
00090   if ( mTodo ) {
00091     readTodo( mTodo, mCalendar, QDate() );
00092   }
00093 }
00094 
00095 void KOTodoEditor::setupGeneral()
00096 {
00097   mGeneral = new KOEditorGeneralTodo(this);
00098 
00099   if (KOPrefs::instance()->mCompactDialogs) {
00100     QFrame *topFrame = addPage(i18n("General"));
00101 
00102     QBoxLayout *topLayout = new QVBoxLayout(topFrame);
00103     topLayout->setMargin(marginHint());
00104     topLayout->setSpacing(spacingHint());
00105 
00106     mGeneral->initHeader( topFrame, topLayout );
00107     mGeneral->initTime(topFrame,topLayout);
00108     QHBoxLayout *priorityLayout = new QHBoxLayout( topLayout );
00109     mGeneral->initPriority(topFrame,priorityLayout);
00110     topLayout->addStretch(1);
00111 
00112     QFrame *topFrame2 = addPage(i18n("Details"));
00113 
00114     QBoxLayout *topLayout2 = new QVBoxLayout(topFrame2);
00115     topLayout2->setMargin(marginHint());
00116     topLayout2->setSpacing(spacingHint());
00117 
00118     QHBoxLayout *completionLayout = new QHBoxLayout( topLayout2 );
00119     mGeneral->initCompletion(topFrame2,completionLayout);
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     mGeneral->initDescription(topFrame,topLayout);
00133     mGeneral->initAttachments(topFrame,topLayout);
00134     connect( mGeneral, SIGNAL( openURL( const KURL& ) ),
00135              this, SLOT( openURL( const KURL& ) ) );
00136     connect( this, SIGNAL( signalAddAttachments( const QStringList&, const QStringList&, bool ) ),
00137              mGeneral, SLOT( addAttachments( const QStringList&, const QStringList&, bool ) ) );
00138   }
00139   mGeneral->finishSetup();
00140 }
00141 
00142 void KOTodoEditor::setupRecurrence()
00143 {
00144   mRecurrenceDialog = new KOEditorRecurrenceDialog( this );
00145   mRecurrenceDialog->hide();
00146   mRecurrence = mRecurrenceDialog->editor();
00147 }
00148 
00149 void KOTodoEditor::editIncidence(Incidence *incidence, const QDate &date, Calendar *calendar)
00150 {
00151   kdDebug(5850) << k_funcinfo << endl;
00152   Todo *todo=dynamic_cast<Todo*>(incidence);
00153   if (todo)
00154   {
00155     init();
00156 
00157     mTodo = todo;
00158     mCalendar = calendar;
00159     readTodo( mTodo, mCalendar, date );
00160   }
00161 
00162   setCaption( i18n("Edit To-do") );
00163 }
00164 
00165 void KOTodoEditor::newTodo()
00166 {
00167   kdDebug(5850) << k_funcinfo << endl;
00168   init();
00169   mTodo = 0;
00170   mCalendar = 0;
00171   setCaption( i18n("New To-do") );
00172   loadDefaults();
00173 }
00174 
00175 void KOTodoEditor::setTexts( const QString &summary, const QString &description )
00176 {
00177   if ( description.isEmpty() && summary.contains("\n") ) {
00178     mGeneral->setDescription( summary );
00179     int pos = summary.find( "\n" );
00180     mGeneral->setSummary( summary.left( pos ) );
00181   } else {
00182     mGeneral->setSummary( summary );
00183     mGeneral->setDescription( description );
00184   }
00185 }
00186 
00187 void KOTodoEditor::loadDefaults()
00188 {
00189   kdDebug(5850) << k_funcinfo << endl;
00190   setDates( QDateTime::currentDateTime().addDays(7), true, 0 );
00191   mGeneral->toggleAlarm( KOPrefs::instance()->defaultTodoReminders() );
00192 }
00193 
00194 bool KOTodoEditor::processInput()
00195 {
00196   if ( !validateInput() ) return false;
00197 
00198   if ( mTodo ) {
00199     bool rc = true;
00200     Todo *oldTodo = mTodo->clone();
00201     Todo *todo = mTodo->clone();
00202 
00203     kdDebug(5850) << "KOTodoEditor::processInput() write event." << endl;
00204     writeTodo( todo );
00205     kdDebug(5850) << "KOTodoEditor::processInput() event written." << endl;
00206 
00207     if( *mTodo == *todo )
00208       // Don't do anything
00209       kdDebug(5850) << "Todo not changed\n";
00210     else {
00211       kdDebug(5850) << "Todo changed\n";
00212       //IncidenceChanger::assignIncidence( mTodo, todo );
00213       writeTodo( mTodo );
00214       mChanger->changeIncidence( oldTodo, mTodo, KOGlobals::NOTHING_MODIFIED, this );
00215     }
00216     delete todo;
00217     delete oldTodo;
00218     return rc;
00219 
00220   } else {
00221     mTodo = new Todo;
00222     mTodo->setOrganizer( Person( KOPrefs::instance()->fullName(),
00223                          KOPrefs::instance()->email() ) );
00224 
00225     writeTodo( mTodo );
00226 
00227     if ( !mChanger->addIncidence( mTodo, mResource, mSubResource, this ) ) {
00228       delete mTodo;
00229       mTodo = 0;
00230       return false;
00231     }
00232   }
00233 
00234   return true;
00235 
00236 }
00237 
00238 void KOTodoEditor::deleteTodo()
00239 {
00240   if (mTodo)
00241     emit deleteIncidenceSignal( mTodo );
00242   emit dialogClose(mTodo);
00243   reject();
00244 }
00245 
00246 void KOTodoEditor::setDates( const QDateTime &due, bool allDay, Todo *relatedEvent )
00247 {
00248   mRelatedTodo = relatedEvent;
00249 
00250   // inherit some properties from parent todo
00251   if ( mRelatedTodo ) {
00252     mGeneral->setCategories( mRelatedTodo->categories() );
00253   }
00254   if ( !due.isValid() && mRelatedTodo && mRelatedTodo->hasDueDate() ) {
00255     mGeneral->setDefaults( mRelatedTodo->dtDue(), allDay );
00256   } else {
00257     mGeneral->setDefaults( due, allDay );
00258   }
00259 
00260   mDetails->setDefaults();
00261   if ( mTodo )
00262     mRecurrence->setDefaults( mTodo->dtStart(), due, false );
00263   else
00264     mRecurrence->setDefaults( QDateTime::currentDateTime(), due, false );
00265 }
00266 
00267 void KOTodoEditor::readTodo( Todo *todo, Calendar *calendar, const QDate &date )
00268 {
00269   if ( !todo ) return;
00270 //   mRelatedTodo = todo->relatedTo();
00271   kdDebug(5850)<<"read todo"<<endl;
00272   mGeneral->readTodo( todo, calendar, date );
00273   mDetails->readEvent( todo );
00274   mRecurrence->readIncidence( todo );
00275 
00276   createEmbeddedURLPages( todo );
00277   readDesignerFields( todo );
00278 }
00279 
00280 void KOTodoEditor::writeTodo( Todo *todo )
00281 {
00282   Incidence *oldIncidence = todo->clone();
00283 
00284   mRecurrence->writeIncidence( todo );
00285   mGeneral->writeTodo( todo );
00286   mDetails->writeEvent( todo );
00287 
00288   if ( *(oldIncidence->recurrence()) != *(todo->recurrence() ) ) {
00289     todo->setDtDue( todo->dtDue(), true );
00290     if ( todo->hasStartDate() )
00291       todo->setDtStart( todo->dtStart() );
00292   }
00293   writeDesignerFields( todo );
00294 
00295   // set related incidence, i.e. parent to-do in this case.
00296   if ( mRelatedTodo ) {
00297     todo->setRelatedTo( mRelatedTodo );
00298   }
00299 
00300   cancelRemovedAttendees( todo );
00301 }
00302 
00303 bool KOTodoEditor::validateInput()
00304 {
00305   if ( !mGeneral->validateInput() ) return false;
00306   if ( !mRecurrence->validateInput() ) return false;
00307   if ( !mDetails->validateInput() ) return false;
00308   return true;
00309 }
00310 
00311 int KOTodoEditor::msgItemDelete()
00312 {
00313   return KMessageBox::warningContinueCancel(this,
00314       i18n("This item will be permanently deleted."),
00315       i18n("KOrganizer Confirmation"), KStdGuiItem::del() );
00316 }
00317 
00318 void KOTodoEditor::modified()
00319 {
00320   // Play dump, just reload the todo. This dialog has become so complicated
00321   // that there is no point in trying to be smart here...
00322   reload();
00323 }
00324 
00325 void KOTodoEditor::loadTemplate( /*const*/ CalendarLocal& cal )
00326 {
00327   Todo::List todos = cal.todos();
00328   if ( todos.count() == 0 ) {
00329     KMessageBox::error( this,
00330         i18n("Template does not contain a valid to-do.") );
00331   } else {
00332     readTodo( todos.first(), 0, QDate() );
00333   }
00334 }
00335 
00336 void KOTodoEditor::slotSaveTemplate( const QString &templateName )
00337 {
00338   Todo *todo = new Todo;
00339   writeTodo( todo );
00340   saveAsTemplate( todo, templateName );
00341 }
00342 
00343 QStringList& KOTodoEditor::templates() const
00344 {
00345   return KOPrefs::instance()->mTodoTemplates;
00346 }
00347 
00348 void KOTodoEditor::updateRecurrenceSummary()
00349 {
00350   Todo *todo = new Todo();
00351   writeTodo( todo );
00352   mGeneral->updateRecurrenceSummary( todo );
00353   delete todo;
00354 }
00355 
00356 #include "kotodoeditor.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys