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