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
54KOTodoEditor::KOTodoEditor( Calendar *calendar, TQWidget *parent ) :
55 KOIncidenceEditor( TQString(), calendar, parent ),
56 mTodo( 0 ), mCalendar( 0 ), mRelatedTodo( 0 ), mGeneral( 0 ), mRecurrence( 0 )
57{
58}
59
60KOTodoEditor::~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
79 connect( mDetails, TQ_SIGNAL(updateAttendeeSummary(int)),
80 mGeneral, TQ_SLOT(updateAttendeeSummary(int)) );
81
82 connect( mGeneral, TQ_SIGNAL(editRecurrence()),
83 mRecurrenceDialog, TQ_SLOT(show()) );
84 connect( mRecurrenceDialog, TQ_SIGNAL(okClicked()),
85 TQ_SLOT(updateRecurrenceSummary()) );
86}
87
88void KOTodoEditor::reload()
89{
90 if ( mTodo ) {
91 readTodo( mTodo, mCalendar, TQDate() );
92 }
93}
94
95void KOTodoEditor::setupGeneral()
96{
97 mGeneral = new KOEditorGeneralTodo(this);
98
99 if (KOPrefs::instance()->mCompactDialogs) {
100 TQFrame *topFrame = addPage(i18n("General"));
101
102 TQBoxLayout *topLayout = new TQVBoxLayout(topFrame);
103 topLayout->setMargin(marginHint());
104 topLayout->setSpacing(spacingHint());
105
106 mGeneral->initHeader( topFrame, topLayout );
107 mGeneral->initTime(topFrame,topLayout);
108 TQHBoxLayout *priorityLayout = new TQHBoxLayout( topLayout );
109 mGeneral->initPriority(topFrame,priorityLayout);
110 topLayout->addStretch(1);
111
112 TQFrame *topFrame2 = addPage(i18n("Details"));
113
114 TQBoxLayout *topLayout2 = new TQVBoxLayout(topFrame2);
115 topLayout2->setMargin(marginHint());
116 topLayout2->setSpacing(spacingHint());
117
118 TQHBoxLayout *completionLayout = new TQHBoxLayout( topLayout2 );
119 mGeneral->initCompletion(topFrame2,completionLayout);
120
121 mGeneral->initSecrecy( topFrame2, topLayout2 );
122 mGeneral->initDescription(topFrame2,topLayout2);
123 } else {
124 TQFrame *topFrame = addPage(i18n("&General"));
125
126 TQBoxLayout *topLayout = new TQVBoxLayout(topFrame);
127 topLayout->setSpacing(spacingHint());
128
129 mGeneral->initHeader( topFrame, topLayout );
130 mGeneral->initTime(topFrame,topLayout);
131 mGeneral->iniStatus(topFrame,topLayout);
132 mGeneral->initDescription(topFrame,topLayout);
133 mGeneral->initAttachments(topFrame,topLayout);
134 connect( mGeneral, TQ_SIGNAL( openURL( const KURL& ) ),
135 this, TQ_SLOT( openURL( const KURL& ) ) );
136 connect( this, TQ_SIGNAL( signalAddAttachments( const TQStringList&, const TQStringList&, bool ) ),
137 mGeneral, TQ_SLOT( addAttachments( const TQStringList&, const TQStringList&, bool ) ) );
138 }
139 mGeneral->finishSetup();
140}
141
142void KOTodoEditor::setupRecurrence()
143{
144 mRecurrenceDialog = new KOEditorRecurrenceDialog( this );
145 mRecurrenceDialog->hide();
146 mRecurrence = mRecurrenceDialog->editor();
147}
148
149void KOTodoEditor::editIncidence( Incidence *incidence, const TQDate &date, Calendar *calendar )
150{
151 kdDebug(5850) << k_funcinfo << endl;
152 Todo *todo = dynamic_cast<Todo*>( incidence );
153 if ( todo ) {
154 init();
155 mTodo = todo;
156 mCalendar = calendar;
157 readTodo( mTodo, mCalendar, date );
158 }
159
160 setCaption( i18n("Edit To-do") );
161}
162
164{
165 kdDebug(5850) << k_funcinfo << endl;
166 init();
167 mTodo = 0;
168 mCalendar = 0;
169 setCaption( i18n("New To-do") );
170 loadDefaults();
171}
172
173void KOTodoEditor::setTexts( const TQString &summary, const TQString &description )
174{
175 if ( description.isEmpty() && summary.contains("\n") ) {
176 mGeneral->setDescription( summary );
177 int pos = summary.find( "\n" );
178 mGeneral->setSummary( summary.left( pos ) );
179 } else {
180 mGeneral->setSummary( summary );
181 mGeneral->setDescription( description );
182 }
183}
184
185void KOTodoEditor::loadDefaults()
186{
187 kdDebug(5850) << k_funcinfo << endl;
188 setDates( TQDateTime::currentDateTime().addDays( 7 ), true, 0 );
189 mGeneral->toggleAlarm( KOPrefs::instance()->defaultTodoReminders() );
190}
191
193{
194 if ( !validateInput() ) {
195 return false;
196 }
197
198 if ( mTodo ) {
199 bool rc = true;
200 Todo *oldTodo = mTodo->clone();
201 Todo *todo = mTodo->clone();
202
203 kdDebug(5850) << "KOTodoEditor::processInput() write event." << endl;
204 writeTodo( todo );
205 kdDebug(5850) << "KOTodoEditor::processInput() event written." << endl;
206
207 if ( *mTodo == *todo ) {
208 // Don't do anything
209 kdDebug(5850) << "Todo not changed\n";
210 } else {
211 kdDebug(5850) << "Todo changed\n";
212 //IncidenceChanger::assignIncidence( mTodo, todo );
213 writeTodo( mTodo );
214
215 KOGlobals::WhatChanged whatChanged;
216
217 if ( !oldTodo->isCompleted() && todo->isCompleted() ) {
218 whatChanged = KOGlobals::COMPLETION_MODIFIED;
219 } else {
220 whatChanged = KOGlobals::NOTHING_MODIFIED;
221 }
222
223 mChanger->changeIncidence( oldTodo, mTodo, whatChanged, this );
224 }
225 delete todo;
226 delete oldTodo;
227 return rc;
228
229 } else {
230 mTodo = new Todo;
231 mTodo->setOrganizer( Person( KOPrefs::instance()->fullName(),
232 KOPrefs::instance()->email() ) );
233
234 writeTodo( mTodo );
235
236 if ( !mChanger->addIncidence( mTodo, mResource, mSubResource, this ) ) {
237 delete mTodo;
238 mTodo = 0;
239 return false;
240 }
241 }
242
243 return true;
244
245}
246
247void KOTodoEditor::deleteTodo()
248{
249 if ( mTodo ) {
250 emit deleteIncidenceSignal( mTodo );
251 }
252 emit dialogClose( mTodo );
253 reject();
254}
255
256void KOTodoEditor::setDates( const TQDateTime &due, bool allDay, Todo *relatedEvent )
257{
258 mRelatedTodo = relatedEvent;
259
260 // inherit some properties from parent todo
261 if ( mRelatedTodo ) {
262 mGeneral->setCategories( mRelatedTodo->categories() );
263 }
264 if ( !due.isValid() && mRelatedTodo && mRelatedTodo->hasDueDate() ) {
265 mGeneral->setDefaults( mRelatedTodo->dtDue(), allDay );
266 } else {
267 mGeneral->setDefaults( due, allDay );
268 }
269
270 mDetails->setDefaults();
271 if ( mTodo ) {
272 mRecurrence->setDefaults( mTodo->dtStart(), due, false );
273 } else {
274 mRecurrence->setDefaults( TQDateTime::currentDateTime(), due, false );
275 }
276}
277
278void KOTodoEditor::readTodo( Todo *todo, Calendar *calendar, const TQDate &date )
279{
280 if ( !todo ) {
281 return;
282 }
283// mRelatedTodo = todo->relatedTo();
284
285 mGeneral->readTodo( todo, calendar, date );
286 mDetails->readEvent( todo );
287 mRecurrence->readIncidence( todo );
288
289 createEmbeddedURLPages( todo );
290 readDesignerFields( todo );
291}
292
294{
295 Incidence *oldIncidence = todo->clone();
296
297 mRecurrence->writeIncidence( todo );
298 mGeneral->writeTodo( todo );
299 mDetails->writeEvent( todo );
300
301 if ( *(oldIncidence->recurrence()) != *(todo->recurrence() ) ) {
302 todo->setDtDue( todo->dtDue(), true );
303 if ( todo->hasStartDate() ) {
304 todo->setDtStart( todo->dtStart() );
305 }
306 }
307 writeDesignerFields( todo );
308
309 // set related incidence, i.e. parent to-do in this case.
310 if ( mRelatedTodo ) {
311 todo->setRelatedTo( mRelatedTodo );
312 }
313
314 cancelRemovedAttendees( todo );
315}
316
318{
319 if ( !mGeneral->validateInput() ) return false;
320 if ( !mRecurrence->validateInput() ) return false;
321 if ( !mDetails->validateInput() ) return false;
322 return true;
323}
324
325int KOTodoEditor::msgItemDelete()
326{
327 return KMessageBox::warningContinueCancel(this,
328 i18n("This item will be permanently deleted."),
329 i18n("KOrganizer Confirmation"), KStdGuiItem::del() );
330}
331
333{
334 // Play dump, just reload the todo. This dialog has become so complicated
335 // that there is no point in trying to be smart here...
336 reload();
337}
338
339void KOTodoEditor::loadTemplate( /*const*/ CalendarLocal& cal )
340{
341 Todo::List todos = cal.todos();
342 if ( todos.count() == 0 ) {
343 KMessageBox::error( this,
344 i18n("Template does not contain a valid to-do.") );
345 } else {
346 readTodo( todos.first(), 0, TQDate() );
347 }
348}
349
350void KOTodoEditor::slotSaveTemplate( const TQString &templateName )
351{
352 Todo *todo = new Todo;
353 writeTodo( todo );
354 saveAsTemplate( todo, templateName );
355}
356
357TQStringList& KOTodoEditor::templates() const
358{
359 return KOPrefs::instance()->mTodoTemplates;
360}
361
362void KOTodoEditor::updateRecurrenceSummary()
363{
364 Todo *todo = new Todo();
365 writeTodo( todo );
366 mGeneral->updateRecurrenceSummary( todo );
367 delete todo;
368}
369
370#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.