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