korganizer

kotodoeditor.cpp
1 /*
2  This file is part of KOrganizer.
3 
4  Copyright (c) 1997, 1998 Preston Brown <pbrown@kde.org>
5  Copyright (c) 2000-2003 Cornelius Schumacher <schumacher@kde.org>
6  Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
7 
8  This program is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 
22  As a special exception, permission is given to link this program
23  with any edition of TQt, and distribute the resulting executable,
24  without including the source code for TQt in the source distribution.
25 */
26 
27 #include <tqtooltip.h>
28 #include <tqframe.h>
29 #include <tqpixmap.h>
30 #include <tqlayout.h>
31 #include <tqdatetime.h>
32 
33 #include <kiconloader.h>
34 #include <tdelocale.h>
35 #include <tdemessagebox.h>
36 
37 #include <libkcal/calendarlocal.h>
39 #include <libkcal/resourcecalendar.h>
40 
41 #include "koprefs.h"
42 #include "koeditorattachments.h"
43 #include "kogroupware.h"
44 #include "kodialogmanager.h"
45 #include "incidencechanger.h"
46 
47 #include "koeditorgeneraltodo.h"
48 #include "koeditordetails.h"
49 #include "koeditorrecurrence.h"
50 
51 #include "kotodoeditor.h"
52 #include "kocore.h"
53 
54 KOTodoEditor::KOTodoEditor( Calendar *calendar, TQWidget *parent ) :
55  KOIncidenceEditor( TQString(), calendar, parent ),
56  mTodo( 0 ), mCalendar( 0 ), mRelatedTodo( 0 ), mGeneral( 0 ), mRecurrence( 0 )
57 {
58 }
59 
60 KOTodoEditor::~KOTodoEditor()
61 {
62  emit dialogClose( mTodo );
63 }
64 
66 {
67  setupGeneral();
68  setupRecurrence();
69  setupAttendeesTab();
70 
71  connect( mGeneral, TQ_SIGNAL( dateTimeStrChanged( const TQString & ) ),
72  mRecurrence, TQ_SLOT( setDateTimeStr( const TQString & ) ) );
73  connect( mGeneral, TQ_SIGNAL( signalDateTimeChanged( const TQDateTime &, const TQDateTime & ) ),
74  mRecurrence, TQ_SLOT( setDateTimes( const TQDateTime &, const TQDateTime & ) ) );
75 
76  connect( mGeneral, TQ_SIGNAL( openCategoryDialog() ),
77  TQ_SIGNAL( editCategories() ) );
78  connect( this, TQ_SIGNAL( updateCategoryConfig() ),
79  mGeneral, TQ_SIGNAL( updateCategoryConfig() ) );
80 
81  connect( mDetails, TQ_SIGNAL(updateAttendeeSummary(int)),
82  mGeneral, TQ_SLOT(updateAttendeeSummary(int)) );
83 
84  connect( mGeneral, TQ_SIGNAL(editRecurrence()),
85  mRecurrenceDialog, TQ_SLOT(show()) );
86  connect( mRecurrenceDialog, TQ_SIGNAL(okClicked()),
87  TQ_SLOT(updateRecurrenceSummary()) );
88 }
89 
90 void KOTodoEditor::reload()
91 {
92  if ( mTodo ) {
93  readTodo( mTodo, mCalendar, TQDate() );
94  }
95 }
96 
97 void KOTodoEditor::setupGeneral()
98 {
99  mGeneral = new KOEditorGeneralTodo(this);
100 
101  if (KOPrefs::instance()->mCompactDialogs) {
102  TQFrame *topFrame = addPage(i18n("General"));
103 
104  TQBoxLayout *topLayout = new TQVBoxLayout(topFrame);
105  topLayout->setMargin(marginHint());
106  topLayout->setSpacing(spacingHint());
107 
108  mGeneral->initHeader( topFrame, topLayout );
109  mGeneral->initTime(topFrame,topLayout);
110  TQHBoxLayout *priorityLayout = new TQHBoxLayout( topLayout );
111  mGeneral->initPriority(topFrame,priorityLayout);
112  topLayout->addStretch(1);
113 
114  TQFrame *topFrame2 = addPage(i18n("Details"));
115 
116  TQBoxLayout *topLayout2 = new TQVBoxLayout(topFrame2);
117  topLayout2->setMargin(marginHint());
118  topLayout2->setSpacing(spacingHint());
119 
120  TQHBoxLayout *completionLayout = new TQHBoxLayout( topLayout2 );
121  mGeneral->initCompletion(topFrame2,completionLayout);
122 
123  mGeneral->initSecrecy( topFrame2, topLayout2 );
124  mGeneral->initDescription(topFrame2,topLayout2);
125  } else {
126  TQFrame *topFrame = addPage(i18n("&General"));
127 
128  TQBoxLayout *topLayout = new TQVBoxLayout(topFrame);
129  topLayout->setSpacing(spacingHint());
130 
131  mGeneral->initHeader( topFrame, topLayout );
132  mGeneral->initTime(topFrame,topLayout);
133  mGeneral->iniStatus(topFrame,topLayout);
134  mGeneral->initDescription(topFrame,topLayout);
135  mGeneral->initAttachments(topFrame,topLayout);
136  connect( mGeneral, TQ_SIGNAL( openURL( const KURL& ) ),
137  this, TQ_SLOT( openURL( const KURL& ) ) );
138  connect( this, TQ_SIGNAL( signalAddAttachments( const TQStringList&, const TQStringList&, bool ) ),
139  mGeneral, TQ_SLOT( addAttachments( const TQStringList&, const TQStringList&, bool ) ) );
140  }
141  mGeneral->finishSetup();
142 }
143 
144 void KOTodoEditor::setupRecurrence()
145 {
146  mRecurrenceDialog = new KOEditorRecurrenceDialog( this );
147  mRecurrenceDialog->hide();
148  mRecurrence = mRecurrenceDialog->editor();
149 }
150 
151 void KOTodoEditor::editIncidence( Incidence *incidence, const TQDate &date, Calendar *calendar )
152 {
153  kdDebug(5850) << k_funcinfo << endl;
154  Todo *todo = dynamic_cast<Todo*>( incidence );
155  if ( todo ) {
156  init();
157  mTodo = todo;
158  mCalendar = calendar;
159  readTodo( mTodo, mCalendar, date );
160  }
161 
162  setCaption( i18n("Edit To-do") );
163 }
164 
166 {
167  kdDebug(5850) << k_funcinfo << endl;
168  init();
169  mTodo = 0;
170  mCalendar = 0;
171  setCaption( i18n("New To-do") );
172  loadDefaults();
173 }
174 
175 void KOTodoEditor::setTexts( const TQString &summary, const TQString &description )
176 {
177  if ( description.isEmpty() && summary.contains("\n") ) {
178  mGeneral->setDescription( summary );
179  int pos = summary.find( "\n" );
180  mGeneral->setSummary( summary.left( pos ) );
181  } else {
182  mGeneral->setSummary( summary );
183  mGeneral->setDescription( description );
184  }
185 }
186 
187 void KOTodoEditor::loadDefaults()
188 {
189  kdDebug(5850) << k_funcinfo << endl;
190  setDates( TQDateTime::currentDateTime().addDays( 7 ), true, 0 );
191  mGeneral->toggleAlarm( KOPrefs::instance()->defaultTodoReminders() );
192 }
193 
195 {
196  if ( !validateInput() ) {
197  return false;
198  }
199 
200  if ( mTodo ) {
201  bool rc = true;
202  Todo *oldTodo = mTodo->clone();
203  Todo *todo = mTodo->clone();
204 
205  kdDebug(5850) << "KOTodoEditor::processInput() write event." << endl;
206  writeTodo( todo );
207  kdDebug(5850) << "KOTodoEditor::processInput() event written." << endl;
208 
209  if ( *mTodo == *todo ) {
210  // Don't do anything
211  kdDebug(5850) << "Todo not changed\n";
212  } else {
213  kdDebug(5850) << "Todo changed\n";
214  //IncidenceChanger::assignIncidence( mTodo, todo );
215  writeTodo( mTodo );
216 
217  KOGlobals::WhatChanged whatChanged;
218 
219  if ( !oldTodo->isCompleted() && todo->isCompleted() ) {
220  whatChanged = KOGlobals::COMPLETION_MODIFIED;
221  } else {
222  whatChanged = KOGlobals::NOTHING_MODIFIED;
223  }
224 
225  mChanger->changeIncidence( oldTodo, mTodo, whatChanged, this );
226  }
227  delete todo;
228  delete oldTodo;
229  return rc;
230 
231  } else {
232  mTodo = new Todo;
233  mTodo->setOrganizer( Person( KOPrefs::instance()->fullName(),
234  KOPrefs::instance()->email() ) );
235 
236  writeTodo( mTodo );
237 
238  if ( !mChanger->addIncidence( mTodo, mResource, mSubResource, this ) ) {
239  delete mTodo;
240  mTodo = 0;
241  return false;
242  }
243  }
244 
245  return true;
246 
247 }
248 
249 void KOTodoEditor::deleteTodo()
250 {
251  if ( mTodo ) {
252  emit deleteIncidenceSignal( mTodo );
253  }
254  emit dialogClose( mTodo );
255  reject();
256 }
257 
258 void KOTodoEditor::setDates( const TQDateTime &due, bool allDay, Todo *relatedEvent )
259 {
260  mRelatedTodo = relatedEvent;
261 
262  // inherit some properties from parent todo
263  if ( mRelatedTodo ) {
264  mGeneral->setCategories( mRelatedTodo->categories() );
265  }
266  if ( !due.isValid() && mRelatedTodo && mRelatedTodo->hasDueDate() ) {
267  mGeneral->setDefaults( mRelatedTodo->dtDue(), allDay );
268  } else {
269  mGeneral->setDefaults( due, allDay );
270  }
271 
272  mDetails->setDefaults();
273  if ( mTodo ) {
274  mRecurrence->setDefaults( mTodo->dtStart(), due, false );
275  } else {
276  mRecurrence->setDefaults( TQDateTime::currentDateTime(), due, false );
277  }
278 }
279 
280 void KOTodoEditor::readTodo( Todo *todo, Calendar *calendar, const TQDate &date )
281 {
282  if ( !todo ) {
283  return;
284  }
285 // mRelatedTodo = todo->relatedTo();
286 
287  mGeneral->readTodo( todo, calendar, date );
288  mDetails->readEvent( todo );
289  mRecurrence->readIncidence( todo );
290 
291  createEmbeddedURLPages( todo );
292  readDesignerFields( todo );
293 }
294 
296 {
297  Incidence *oldIncidence = todo->clone();
298 
299  mRecurrence->writeIncidence( todo );
300  mGeneral->writeTodo( todo );
301  mDetails->writeEvent( todo );
302 
303  if ( *(oldIncidence->recurrence()) != *(todo->recurrence() ) ) {
304  todo->setDtDue( todo->dtDue(), true );
305  if ( todo->hasStartDate() ) {
306  todo->setDtStart( todo->dtStart() );
307  }
308  }
309  writeDesignerFields( todo );
310 
311  // set related incidence, i.e. parent to-do in this case.
312  if ( mRelatedTodo ) {
313  todo->setRelatedTo( mRelatedTodo );
314  }
315 
316  cancelRemovedAttendees( todo );
317 }
318 
320 {
321  if ( !mGeneral->validateInput() ) return false;
322  if ( !mRecurrence->validateInput() ) return false;
323  if ( !mDetails->validateInput() ) return false;
324  return true;
325 }
326 
327 int KOTodoEditor::msgItemDelete()
328 {
329  return KMessageBox::warningContinueCancel(this,
330  i18n("This item will be permanently deleted."),
331  i18n("KOrganizer Confirmation"), KStdGuiItem::del() );
332 }
333 
335 {
336  // Play dump, just reload the todo. This dialog has become so complicated
337  // that there is no point in trying to be smart here...
338  reload();
339 }
340 
341 void KOTodoEditor::loadTemplate( /*const*/ CalendarLocal& cal )
342 {
343  Todo::List todos = cal.todos();
344  if ( todos.count() == 0 ) {
345  KMessageBox::error( this,
346  i18n("Template does not contain a valid to-do.") );
347  } else {
348  readTodo( todos.first(), 0, TQDate() );
349  }
350 }
351 
352 void KOTodoEditor::slotSaveTemplate( const TQString &templateName )
353 {
354  Todo *todo = new Todo;
355  writeTodo( todo );
356  saveAsTemplate( todo, templateName );
357 }
358 
359 TQStringList& KOTodoEditor::templates() const
360 {
361  return KOPrefs::instance()->mTodoTemplates;
362 }
363 
364 void KOTodoEditor::updateRecurrenceSummary()
365 {
366  Todo *todo = new Todo();
367  writeTodo( todo );
368  mGeneral->updateRecurrenceSummary( todo );
369  delete todo;
370 }
371 
372 #include "kotodoeditor.moc"
virtual Todo::List todos(TodoSortField sortField=TodoSortUnsorted, SortDirection sortDirection=SortDirectionAscending)
void setOrganizer(const Person &o)
void setRelatedTo(Incidence *relatedTo)
TQStringList categories() const
Recurrence * recurrence() const
bool hasDueDate() const
bool isCompleted() const
bool hasStartDate() const
Todo * clone()
void setDtDue(const TQDateTime &dtDue, bool first=false)
TQDateTime dtStart(bool first=false) const
void setDtStart(const TQDateTime &dtStart)
TQDateTime dtDue(bool first=false) const
This is the base class for the calendar component editors.
void addAttachments(const TQStringList &attachments, const TQStringList &mimeTypes=TQStringList(), bool inlineAttachment=false)
Adds attachments to the editor.
void readTodo(Todo *todo, Calendar *calendar, const TQDate &date)
Read event object and setup widgets accordingly.
void init()
Initialize editor.
void setTexts(const TQString &summary, const TQString &description=TQString())
Sets the given summary and description.
void setDates(const TQDateTime &due, bool allDay=true, Todo *relatedTodo=0)
Set widgets to default values.
void newTodo()
Edit new todo.
KOTodoEditor(Calendar *calendar, TQWidget *parent)
Constructs a new todo editor.
void writeTodo(Todo *)
Write event settings to event object.
bool processInput()
Process user input and create or update event.
bool validateInput()
Check if the input is valid.
void editIncidence(Incidence *incidence, const TQDate &date, Calendar *calendar)
Edit an existing todo.
void modified()
This todo has been modified externally.