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