1 /* This file is part of the KDE project
2  * Copyright (C) 2004 - 2007 Dag Andersen <danders@get2net.dk>
3  * Copyright (C) 2011 Dag Andersen <danders@get2net.dk>
4  * Copyright (C) 2016 Dag Andersen <danders@get2net.dk>
5  * Copyright (C) 2019 Dag Andersen <danders@get2net.dk>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 // clazy:excludeall=qstring-arg
24 #include "kptcommand.h"
25 #include "kptaccount.h"
26 #include "kptappointment.h"
27 #include "kptproject.h"
28 #include "kpttask.h"
29 #include "kptcalendar.h"
30 #include "kptrelation.h"
31 #include "kptresource.h"
32 #include "kptdocuments.h"
33 #include "kptlocale.h"
34 #include "kptdebug.h"
35 
36 #include <QApplication>
37 
38 
PLANCMDINSPROJECT_LOG()39 const QLoggingCategory &PLANCMDINSPROJECT_LOG()
40 {
41     static const QLoggingCategory category("calligra.plan.command.insertProject");
42     return category;
43 }
44 
45 #define debugPlanInsertProject qCDebug(PLANCMDINSPROJECT_LOG)
46 #define warnPlanInsertProject qCWarning(PLANCMDINSPROJECT_LOG)
47 #define errorPlanInsertProject qCCritical(PLANCMDINSPROJECT_LOG)
48 
49 
50 namespace KPlato
51 {
52 
setSchScheduled()53 void NamedCommand::setSchScheduled()
54 {
55     QHash<Schedule*, bool>::ConstIterator it;
56     for (it = m_schedules.constBegin(); it != m_schedules.constEnd(); ++it) {
57         //debugPlan << it.key() ->name() <<":" << it.value();
58         it.key() ->setScheduled(it.value());
59     }
60 }
setSchScheduled(bool state)61 void NamedCommand::setSchScheduled(bool state)
62 {
63     QHash<Schedule*, bool>::ConstIterator it;
64     for (it = m_schedules.constBegin(); it != m_schedules.constEnd(); ++it) {
65         //debugPlan << it.key() ->name() <<":" << state;
66         it.key() ->setScheduled(state);
67     }
68 }
addSchScheduled(Schedule * sch)69 void NamedCommand::addSchScheduled(Schedule *sch)
70 {
71     //debugPlan << sch->id() <<":" << sch->isScheduled();
72     m_schedules.insert(sch, sch->isScheduled());
73     foreach (Appointment * a, sch->appointments()) {
74         if (a->node() == sch) {
75             m_schedules.insert(a->resource(), a->resource() ->isScheduled());
76         } else if (a->resource() == sch) {
77             m_schedules.insert(a->node(), a->node() ->isScheduled());
78         }
79     }
80 }
81 
82 //---------
~MacroCommand()83 MacroCommand::~MacroCommand()
84 {
85     while (! cmds.isEmpty()) {
86         delete cmds.takeLast();
87     }
88 }
89 
addCommand(KUndo2Command * cmd)90 void MacroCommand::addCommand(KUndo2Command *cmd)
91 {
92     cmds.append(cmd);
93 }
94 
execute()95 void MacroCommand::execute()
96 {
97     foreach (KUndo2Command *c, cmds) {
98         c->redo();
99     }
100 }
101 
unexecute()102 void MacroCommand::unexecute()
103 {
104     for (int i = cmds.count() - 1; i >= 0; --i) {
105         cmds.at(i)->undo();
106     }
107 }
108 
109 //-------------------------------------------------
CalendarAddCmd(Project * project,Calendar * cal,int pos,Calendar * parent,const KUndo2MagicString & name)110 CalendarAddCmd::CalendarAddCmd(Project *project, Calendar *cal, int pos, Calendar *parent, const KUndo2MagicString& name)
111         : NamedCommand(name),
112         m_project(project),
113         m_cal(cal),
114         m_pos(pos),
115         m_parent(parent),
116         m_mine(true)
117 {
118     //debugPlan<<cal->name();
119     Q_ASSERT(project != 0);
120 }
~CalendarAddCmd()121 CalendarAddCmd::~CalendarAddCmd()
122 {
123     if (m_mine)
124         delete m_cal;
125 }
execute()126 void CalendarAddCmd::execute()
127 {
128     if (m_project) {
129         m_project->addCalendar(m_cal, m_parent, m_pos);
130         m_mine = false;
131     }
132 
133     //debugPlan<<m_cal->name()<<" added to:"<<m_project->name();
134 }
135 
unexecute()136 void CalendarAddCmd::unexecute()
137 {
138     if (m_project) {
139         m_project->takeCalendar(m_cal);
140         m_mine = true;
141     }
142 
143     //debugPlan<<m_cal->name();
144 }
145 
CalendarRemoveCmd(Project * project,Calendar * cal,const KUndo2MagicString & name)146 CalendarRemoveCmd::CalendarRemoveCmd(Project *project, Calendar *cal, const KUndo2MagicString& name)
147         : NamedCommand(name),
148         m_project(project),
149         m_parent(cal->parentCal()),
150         m_cal(cal),
151         m_index(-1),
152         m_mine(false),
153         m_cmd(new MacroCommand(KUndo2MagicString()))
154 {
155     Q_ASSERT(project != 0);
156 
157     m_index = m_parent ? m_parent->indexOf(cal) : project->indexOf(cal);
158 
159     foreach (Resource *r, project->resourceList()) {
160         if (r->calendar(true) == cal) {
161             m_cmd->addCommand(new ModifyResourceCalendarCmd(r, 0));
162         }
163     }
164     if (project->defaultCalendar() == cal) {
165         m_cmd->addCommand(new ProjectModifyDefaultCalendarCmd(project, 0));
166     }
167     foreach (Calendar *c, cal->calendars()) {
168         m_cmd->addCommand(new CalendarRemoveCmd(project, c));
169     }
170 }
~CalendarRemoveCmd()171 CalendarRemoveCmd::~CalendarRemoveCmd()
172 {
173     delete m_cmd;
174     if (m_mine)
175         delete m_cal;
176 }
execute()177 void CalendarRemoveCmd::execute()
178 {
179     m_cmd->execute();
180     m_project->takeCalendar(m_cal);
181     m_mine = true;
182 
183 }
unexecute()184 void CalendarRemoveCmd::unexecute()
185 {
186     m_project->addCalendar(m_cal, m_parent, m_index);
187     m_cmd->unexecute();
188     m_mine = false;
189 
190 }
191 
CalendarMoveCmd(Project * project,Calendar * cal,int position,Calendar * parent,const KUndo2MagicString & name)192 CalendarMoveCmd::CalendarMoveCmd(Project *project, Calendar *cal, int position, Calendar *parent, const KUndo2MagicString& name)
193         : NamedCommand(name),
194         m_project(project),
195         m_cal(cal),
196         m_newpos(position),
197         m_newparent(parent),
198         m_oldparent(cal->parentCal())
199 {
200     //debugPlan<<cal->name();
201     Q_ASSERT(project != 0);
202 
203     m_oldpos = m_oldparent ? m_oldparent->indexOf(cal) : project->indexOf(cal);
204 }
execute()205 void CalendarMoveCmd::execute()
206 {
207     m_project->takeCalendar(m_cal);
208     m_project->addCalendar(m_cal, m_newparent, m_newpos);
209 }
210 
unexecute()211 void CalendarMoveCmd::unexecute()
212 {
213     m_project->takeCalendar(m_cal);
214     m_project->addCalendar(m_cal, m_oldparent, m_oldpos);
215 }
216 
CalendarModifyNameCmd(Calendar * cal,const QString & newvalue,const KUndo2MagicString & name)217 CalendarModifyNameCmd::CalendarModifyNameCmd(Calendar *cal, const QString& newvalue, const KUndo2MagicString& name)
218         : NamedCommand(name),
219         m_cal(cal)
220 {
221 
222     m_oldvalue = cal->name();
223     m_newvalue = newvalue;
224     //debugPlan<<cal->name();
225 }
execute()226 void CalendarModifyNameCmd::execute()
227 {
228     m_cal->setName(m_newvalue);
229 
230     //debugPlan<<m_cal->name();
231 }
unexecute()232 void CalendarModifyNameCmd::unexecute()
233 {
234     m_cal->setName(m_oldvalue);
235 
236     //debugPlan<<m_cal->name();
237 }
238 
CalendarModifyParentCmd(Project * project,Calendar * cal,Calendar * newvalue,const KUndo2MagicString & name)239 CalendarModifyParentCmd::CalendarModifyParentCmd(Project *project, Calendar *cal, Calendar *newvalue, const KUndo2MagicString& name)
240         : NamedCommand(name),
241         m_project(project),
242         m_cal(cal),
243         m_cmd(new MacroCommand(KUndo2MagicString())),
244         m_oldindex(-1),
245         m_newindex(-1)
246 {
247     m_oldvalue = cal->parentCal();
248     m_newvalue = newvalue;
249     m_oldindex = m_oldvalue ? m_oldvalue->indexOf(cal) : m_project->indexOf(cal);
250 
251     if (newvalue) {
252         m_cmd->addCommand(new CalendarModifyTimeZoneCmd(cal, newvalue->timeZone()));
253     }
254     //debugPlan<<cal->name();
255 }
~CalendarModifyParentCmd()256 CalendarModifyParentCmd::~CalendarModifyParentCmd()
257 {
258     delete m_cmd;
259 }
execute()260 void CalendarModifyParentCmd::execute()
261 {
262     m_project->takeCalendar(m_cal);
263     m_project->addCalendar(m_cal, m_newvalue, m_newindex);
264     m_cmd->execute();
265 }
unexecute()266 void CalendarModifyParentCmd::unexecute()
267 {
268     m_cmd->unexecute();
269     m_project->takeCalendar(m_cal);
270     m_project->addCalendar(m_cal, m_oldvalue, m_oldindex);
271 }
272 
CalendarModifyTimeZoneCmd(Calendar * cal,const QTimeZone & value,const KUndo2MagicString & name)273 CalendarModifyTimeZoneCmd::CalendarModifyTimeZoneCmd(Calendar *cal, const QTimeZone &value, const KUndo2MagicString& name)
274         : NamedCommand(name),
275         m_cal(cal),
276         m_newvalue(value),
277         m_cmd(new MacroCommand(KUndo2MagicString()))
278 {
279     m_oldvalue = cal->timeZone();
280     foreach (Calendar *c, cal->calendars()) {
281         m_cmd->addCommand(new CalendarModifyTimeZoneCmd(c, value));
282     }
283     //debugPlan<<cal->name();
284 }
~CalendarModifyTimeZoneCmd()285 CalendarModifyTimeZoneCmd::~CalendarModifyTimeZoneCmd()
286 {
287     delete m_cmd;
288 }
execute()289 void CalendarModifyTimeZoneCmd::execute()
290 {
291     m_cmd->execute();
292     m_cal->setTimeZone(m_newvalue);
293 }
unexecute()294 void CalendarModifyTimeZoneCmd::unexecute()
295 {
296     m_cal->setTimeZone(m_oldvalue);
297     m_cmd->unexecute();
298 }
299 
300 #ifdef HAVE_KHOLIDAYS
CalendarModifyHolidayRegionCmd(Calendar * cal,const QString & value,const KUndo2MagicString & name)301 CalendarModifyHolidayRegionCmd::CalendarModifyHolidayRegionCmd(Calendar *cal, const QString &value, const KUndo2MagicString& name)
302     : NamedCommand(name),
303     m_cal(cal),
304     m_newvalue(value)
305 {
306     m_oldvalue = cal->holidayRegionCode();
307 }
~CalendarModifyHolidayRegionCmd()308 CalendarModifyHolidayRegionCmd::~CalendarModifyHolidayRegionCmd()
309 {
310 }
execute()311 void CalendarModifyHolidayRegionCmd::execute()
312 {
313     m_cal->setHolidayRegion(m_newvalue);
314 }
unexecute()315 void CalendarModifyHolidayRegionCmd::unexecute()
316 {
317     m_cal->setHolidayRegion(m_oldvalue);
318 }
319 #endif
320 
CalendarAddDayCmd(Calendar * cal,CalendarDay * newvalue,const KUndo2MagicString & name)321 CalendarAddDayCmd::CalendarAddDayCmd(Calendar *cal, CalendarDay *newvalue, const KUndo2MagicString& name)
322         : NamedCommand(name),
323         m_cal(cal),
324         m_mine(true)
325 {
326 
327     m_newvalue = newvalue;
328     //debugPlan<<cal->name();
329 }
~CalendarAddDayCmd()330 CalendarAddDayCmd::~CalendarAddDayCmd()
331 {
332     //debugPlan;
333     if (m_mine)
334         delete m_newvalue;
335 }
execute()336 void CalendarAddDayCmd::execute()
337 {
338     //debugPlan<<m_cal->name();
339     m_cal->addDay(m_newvalue);
340     m_mine = false;
341 }
unexecute()342 void CalendarAddDayCmd::unexecute()
343 {
344     //debugPlan<<m_cal->name();
345     m_cal->takeDay(m_newvalue);
346     m_mine = true;
347 }
348 
CalendarRemoveDayCmd(Calendar * cal,CalendarDay * day,const KUndo2MagicString & name)349 CalendarRemoveDayCmd::CalendarRemoveDayCmd(Calendar *cal,CalendarDay *day, const KUndo2MagicString& name)
350         : NamedCommand(name),
351         m_cal(cal),
352         m_value(day),
353         m_mine(false)
354 {
355     //debugPlan<<cal->name();
356     // TODO check if any resources uses this calendar
357     init();
358 }
CalendarRemoveDayCmd(Calendar * cal,const QDate & day,const KUndo2MagicString & name)359 CalendarRemoveDayCmd::CalendarRemoveDayCmd(Calendar *cal, const QDate &day, const KUndo2MagicString& name)
360         : NamedCommand(name),
361         m_cal(cal),
362         m_mine(false)
363 {
364 
365     m_value = cal->findDay(day);
366     //debugPlan<<cal->name();
367     // TODO check if any resources uses this calendar
368     init();
369 }
init()370 void CalendarRemoveDayCmd::init()
371 {
372 }
execute()373 void CalendarRemoveDayCmd::execute()
374 {
375     //debugPlan<<m_cal->name();
376     m_cal->takeDay(m_value);
377     m_mine = true;
378 }
unexecute()379 void CalendarRemoveDayCmd::unexecute()
380 {
381     //debugPlan<<m_cal->name();
382     m_cal->addDay(m_value);
383     m_mine = false;
384 }
385 
CalendarModifyDayCmd(Calendar * cal,CalendarDay * value,const KUndo2MagicString & name)386 CalendarModifyDayCmd::CalendarModifyDayCmd(Calendar *cal, CalendarDay *value, const KUndo2MagicString& name)
387         : NamedCommand(name),
388         m_cal(cal),
389         m_mine(true)
390 {
391 
392     m_newvalue = value;
393     m_oldvalue = cal->findDay(value->date());
394     //debugPlan<<cal->name()<<" old:("<<m_oldvalue<<") new:("<<m_newvalue<<")";
395 }
~CalendarModifyDayCmd()396 CalendarModifyDayCmd::~CalendarModifyDayCmd()
397 {
398     //debugPlan;
399     if (m_mine) {
400         delete m_newvalue;
401     } else {
402         delete m_oldvalue;
403     }
404 }
execute()405 void CalendarModifyDayCmd::execute()
406 {
407     //debugPlan;
408     if (m_oldvalue) {
409         m_cal->takeDay(m_oldvalue);
410     }
411     m_cal->addDay(m_newvalue);
412     m_mine = false;
413 }
unexecute()414 void CalendarModifyDayCmd::unexecute()
415 {
416     //debugPlan;
417     m_cal->takeDay(m_newvalue);
418     if (m_oldvalue) {
419         m_cal->addDay(m_oldvalue);
420     }
421     m_mine = true;
422 }
423 
CalendarModifyStateCmd(Calendar * calendar,CalendarDay * day,CalendarDay::State value,const KUndo2MagicString & name)424 CalendarModifyStateCmd::CalendarModifyStateCmd(Calendar *calendar, CalendarDay *day, CalendarDay::State value, const KUndo2MagicString& name)
425         : NamedCommand(name),
426         m_calendar(calendar),
427         m_day(day),
428         m_cmd(new MacroCommand(KUndo2MagicString()))
429 {
430 
431     m_newvalue = value;
432     m_oldvalue = (CalendarDay::State)day->state();
433     if (value != CalendarDay::Working) {
434         foreach (TimeInterval *ti, day->timeIntervals()) {
435             m_cmd->addCommand(new CalendarRemoveTimeIntervalCmd(calendar, day, ti));
436         }
437     }
438 }
~CalendarModifyStateCmd()439 CalendarModifyStateCmd::~CalendarModifyStateCmd()
440 {
441     delete m_cmd;
442 }
execute()443 void CalendarModifyStateCmd::execute()
444 {
445     //debugPlan;
446     m_cmd->execute();
447     m_calendar->setState(m_day, m_newvalue);
448 
449 }
unexecute()450 void CalendarModifyStateCmd::unexecute()
451 {
452     //debugPlan;
453     m_calendar->setState(m_day, m_oldvalue);
454     m_cmd->unexecute();
455 
456 }
457 
CalendarModifyTimeIntervalCmd(Calendar * calendar,TimeInterval & newvalue,TimeInterval * value,const KUndo2MagicString & name)458 CalendarModifyTimeIntervalCmd::CalendarModifyTimeIntervalCmd(Calendar *calendar, TimeInterval &newvalue, TimeInterval *value, const KUndo2MagicString& name)
459         : NamedCommand(name),
460         m_calendar(calendar)
461 {
462 
463     m_value = value; // keep pointer
464     m_oldvalue = *value; // save value
465     m_newvalue = newvalue;
466 }
execute()467 void CalendarModifyTimeIntervalCmd::execute()
468 {
469     //debugPlan;
470     m_calendar->setWorkInterval(m_value, m_newvalue);
471 
472 }
unexecute()473 void CalendarModifyTimeIntervalCmd::unexecute()
474 {
475     //debugPlan;
476     m_calendar->setWorkInterval(m_value, m_oldvalue);
477 
478 }
479 
CalendarAddTimeIntervalCmd(Calendar * calendar,CalendarDay * day,TimeInterval * value,const KUndo2MagicString & name)480 CalendarAddTimeIntervalCmd::CalendarAddTimeIntervalCmd(Calendar *calendar, CalendarDay *day, TimeInterval *value, const KUndo2MagicString& name)
481     : NamedCommand(name),
482     m_calendar(calendar),
483     m_day(day),
484     m_value(value),
485     m_mine(true)
486 {
487 }
~CalendarAddTimeIntervalCmd()488 CalendarAddTimeIntervalCmd::~CalendarAddTimeIntervalCmd()
489 {
490     if (m_mine)
491         delete m_value;
492 }
execute()493 void CalendarAddTimeIntervalCmd::execute()
494 {
495     //debugPlan;
496     m_calendar->addWorkInterval(m_day, m_value);
497     m_mine = false;
498 
499 }
unexecute()500 void CalendarAddTimeIntervalCmd::unexecute()
501 {
502     //debugPlan;
503     m_calendar->takeWorkInterval(m_day, m_value);
504     m_mine = true;
505 
506 }
507 
CalendarRemoveTimeIntervalCmd(Calendar * calendar,CalendarDay * day,TimeInterval * value,const KUndo2MagicString & name)508 CalendarRemoveTimeIntervalCmd::CalendarRemoveTimeIntervalCmd(Calendar *calendar, CalendarDay *day, TimeInterval *value, const KUndo2MagicString& name)
509     : CalendarAddTimeIntervalCmd(calendar, day, value, name)
510 {
511     m_mine = false ;
512 }
execute()513 void CalendarRemoveTimeIntervalCmd::execute()
514 {
515     CalendarAddTimeIntervalCmd::unexecute();
516 }
unexecute()517 void CalendarRemoveTimeIntervalCmd::unexecute()
518 {
519     CalendarAddTimeIntervalCmd::execute();
520 }
521 
CalendarModifyWeekdayCmd(Calendar * cal,int weekday,CalendarDay * value,const KUndo2MagicString & name)522 CalendarModifyWeekdayCmd::CalendarModifyWeekdayCmd(Calendar *cal, int weekday, CalendarDay *value, const KUndo2MagicString& name)
523         : NamedCommand(name),
524         m_weekday(weekday),
525         m_cal(cal),
526         m_value(value),
527         m_orig(*(cal->weekday(weekday)))
528 {
529 
530     //debugPlan << cal->name() <<" (" << value <<")";
531 }
~CalendarModifyWeekdayCmd()532 CalendarModifyWeekdayCmd::~CalendarModifyWeekdayCmd()
533 {
534     //debugPlan << m_weekday <<":" << m_value;
535     delete m_value;
536 
537 }
execute()538 void CalendarModifyWeekdayCmd::execute()
539 {
540     m_cal->setWeekday(m_weekday, *m_value);
541 }
unexecute()542 void CalendarModifyWeekdayCmd::unexecute()
543 {
544     m_cal->setWeekday(m_weekday, m_orig);
545 }
546 
CalendarModifyDateCmd(Calendar * cal,CalendarDay * day,const QDate & value,const KUndo2MagicString & name)547 CalendarModifyDateCmd::CalendarModifyDateCmd(Calendar *cal, CalendarDay *day, const QDate &value, const KUndo2MagicString& name)
548     : NamedCommand(name),
549     m_cal(cal),
550     m_day(day),
551     m_newvalue(value),
552     m_oldvalue(day->date())
553 {
554     //debugPlan << cal->name() <<" (" << value <<")";
555 }
execute()556 void CalendarModifyDateCmd::execute()
557 {
558     m_cal->setDate(m_day, m_newvalue);
559 }
unexecute()560 void CalendarModifyDateCmd::unexecute()
561 {
562     m_cal->setDate(m_day, m_oldvalue);
563 }
564 
ProjectModifyDefaultCalendarCmd(Project * project,Calendar * cal,const KUndo2MagicString & name)565 ProjectModifyDefaultCalendarCmd::ProjectModifyDefaultCalendarCmd(Project *project, Calendar *cal, const KUndo2MagicString& name)
566     : NamedCommand(name),
567     m_project(project),
568     m_newvalue(cal),
569     m_oldvalue(project->defaultCalendar())
570 {
571     //debugPlan << cal->name() <<" (" << value <<")";
572 }
execute()573 void ProjectModifyDefaultCalendarCmd::execute()
574 {
575     m_project->setDefaultCalendar(m_newvalue);
576 
577 }
unexecute()578 void ProjectModifyDefaultCalendarCmd::unexecute()
579 {
580     m_project->setDefaultCalendar(m_oldvalue);
581 
582 }
583 
NodeDeleteCmd(Node * node,const KUndo2MagicString & name)584 NodeDeleteCmd::NodeDeleteCmd(Node *node, const KUndo2MagicString& name)
585         : NamedCommand(name),
586         m_node(node),
587         m_index(-1),
588         m_relCmd(0)
589 {
590 
591     m_parent = node->parentNode();
592     m_mine = false;
593 
594     m_project = static_cast<Project*>(node->projectNode());
595     if (m_project) {
596         foreach (Schedule * s, m_project->schedules()) {
597             if (s && s->isScheduled()) {
598                 // Only invalidate schedules this node is part of
599                 Schedule *ns = node->findSchedule(s->id());
600                 if (ns && ! ns->isDeleted()) {
601                     addSchScheduled(s);
602                 }
603             }
604         }
605     }
606     m_cmd = new MacroCommand(KUndo2MagicString());
607     QList<Node*> lst = node->childNodeIterator();
608     for (int i = lst.count(); i > 0; --i) {
609         m_cmd->addCommand(new NodeDeleteCmd(lst[ i - 1 ]));
610     }
611     if (node->runningAccount()) {
612         m_cmd->addCommand(new NodeModifyRunningAccountCmd(*node, node->runningAccount(), 0));
613     }
614     if (node->startupAccount()) {
615         m_cmd->addCommand(new NodeModifyRunningAccountCmd(*node, node->startupAccount(), 0));
616     }
617     if (node->shutdownAccount()) {
618         m_cmd->addCommand(new NodeModifyRunningAccountCmd(*node, node->shutdownAccount(), 0));
619     }
620 
621 }
~NodeDeleteCmd()622 NodeDeleteCmd::~NodeDeleteCmd()
623 {
624     delete m_relCmd; // before node
625     if (m_mine) {
626         delete m_node;
627     }
628     delete m_cmd;
629     while (!m_appointments.isEmpty())
630         delete m_appointments.takeFirst();
631 }
execute()632 void NodeDeleteCmd::execute()
633 {
634     if (m_parent && m_project) {
635         m_index = m_parent->findChildNode(m_node);
636         //debugPlan<<m_node->name()<<""<<m_index;
637         if (!m_relCmd) {
638             m_relCmd = new MacroCommand();
639             // Only add delete relation commands if we (still) have relations
640             // The other node might have deleted them...
641             foreach (Relation * r, m_node->dependChildNodes()) {
642                 m_relCmd->addCommand(new DeleteRelationCmd(*m_project, r));
643             }
644             foreach (Relation * r, m_node->dependParentNodes()) {
645                 m_relCmd->addCommand(new DeleteRelationCmd(*m_project, r));
646             }
647         }
648         m_relCmd->execute();
649         if (m_cmd) {
650             m_cmd->execute();
651         }
652         m_project->takeTask(m_node);
653         m_mine = true;
654         setSchScheduled(false);
655     }
656 }
unexecute()657 void NodeDeleteCmd::unexecute()
658 {
659     if (m_parent && m_project) {
660         //debugPlan<<m_node->name()<<""<<m_index;
661         m_project->addSubTask(m_node, m_index, m_parent);
662         if (m_cmd) {
663             m_cmd->unexecute();
664         }
665         m_relCmd->unexecute();
666         m_mine = false;
667         setSchScheduled();
668     }
669 }
670 
TaskAddCmd(Project * project,Node * node,Node * after,const KUndo2MagicString & name)671 TaskAddCmd::TaskAddCmd(Project *project, Node *node, Node *after, const KUndo2MagicString& name)
672         : NamedCommand(name),
673         m_project(project),
674         m_node(node),
675         m_after(after),
676         m_added(false)
677 {
678 
679     // set some reasonable defaults for normally calculated values
680     if (after && after->parentNode() && after->parentNode() != project) {
681         node->setStartTime(after->parentNode() ->startTime());
682         node->setEndTime(node->startTime() + node->duration());
683     } else {
684         if (project->constraint() == Node::MustFinishOn) {
685             node->setEndTime(project->endTime());
686             node->setStartTime(node->endTime() - node->duration());
687         } else {
688             node->setStartTime(project->startTime());
689             node->setEndTime(node->startTime() + node->duration());
690         }
691     }
692     node->setEarlyStart(node->startTime());
693     node->setLateFinish(node->endTime());
694     node->setWorkStartTime(node->startTime());
695     node->setWorkEndTime(node->endTime());
696 }
~TaskAddCmd()697 TaskAddCmd::~TaskAddCmd()
698 {
699     if (!m_added)
700         delete m_node;
701 }
execute()702 void TaskAddCmd::execute()
703 {
704     //debugPlan<<m_node->name();
705     m_project->addTask(m_node, m_after);
706     m_added = true;
707 
708 
709 }
unexecute()710 void TaskAddCmd::unexecute()
711 {
712     m_project->takeTask(m_node);
713     m_added = false;
714 
715 
716 }
717 
SubtaskAddCmd(Project * project,Node * node,Node * parent,const KUndo2MagicString & name)718 SubtaskAddCmd::SubtaskAddCmd(Project *project, Node *node, Node *parent, const KUndo2MagicString& name)
719         : NamedCommand(name),
720         m_project(project),
721         m_node(node),
722         m_parent(parent),
723         m_added(false),
724         m_cmd(0)
725 {
726 
727     // set some reasonable defaults for normally calculated values
728     node->setStartTime(parent->startTime());
729     node->setEndTime(node->startTime() + node->duration());
730     node->setEarlyStart(node->startTime());
731     node->setLateFinish(node->endTime());
732     node->setWorkStartTime(node->startTime());
733     node->setWorkEndTime(node->endTime());
734 
735     // Summarytasks can't have resources, so remove resource requests from the new parent
736     foreach (ResourceGroupRequest *r, parent->requests().requests()) {
737         if (m_cmd == 0) m_cmd = new MacroCommand(KUndo2MagicString());
738         m_cmd->addCommand(new RemoveResourceGroupRequestCmd(r));
739     }
740     // Also remove accounts
741     if (parent->runningAccount()) {
742         if (m_cmd == 0) m_cmd = new MacroCommand(KUndo2MagicString());
743         m_cmd->addCommand(new NodeModifyRunningAccountCmd(*parent, parent->runningAccount(), 0));
744     }
745     if (parent->startupAccount()) {
746         if (m_cmd == 0) m_cmd = new MacroCommand(KUndo2MagicString());
747         m_cmd->addCommand(new NodeModifyStartupAccountCmd(*parent, parent->startupAccount(), 0));
748     }
749     if (parent->shutdownAccount()) {
750         if (m_cmd == 0) m_cmd = new MacroCommand(KUndo2MagicString());
751         m_cmd->addCommand(new NodeModifyShutdownAccountCmd(*parent, parent->shutdownAccount(), 0));
752     }
753 }
~SubtaskAddCmd()754 SubtaskAddCmd::~SubtaskAddCmd()
755 {
756     delete m_cmd;
757     if (!m_added)
758         delete m_node;
759 }
execute()760 void SubtaskAddCmd::execute()
761 {
762     m_project->addSubTask(m_node, m_parent);
763     if (m_cmd) {
764         m_cmd->execute();
765     }
766     m_added = true;
767 
768 
769 }
unexecute()770 void SubtaskAddCmd::unexecute()
771 {
772     m_project->takeTask(m_node);
773     if (m_cmd) {
774         m_cmd->unexecute();
775     }
776     m_added = false;
777 
778 
779 }
780 
NodeModifyNameCmd(Node & node,const QString & nodename,const KUndo2MagicString & name)781 NodeModifyNameCmd::NodeModifyNameCmd(Node &node, const QString& nodename, const KUndo2MagicString& name)
782         : NamedCommand(name),
783         m_node(node),
784         newName(nodename),
785         oldName(node.name())
786 {
787 }
execute()788 void NodeModifyNameCmd::execute()
789 {
790     m_node.setName(newName);
791 
792 
793 }
unexecute()794 void NodeModifyNameCmd::unexecute()
795 {
796     m_node.setName(oldName);
797 
798 
799 }
800 
NodeModifyPriorityCmd(Node & node,int oldValue,int newValue,const KUndo2MagicString & name)801 NodeModifyPriorityCmd::NodeModifyPriorityCmd(Node &node, int oldValue, int newValue, const KUndo2MagicString& name)
802     : NamedCommand(name)
803     , m_node(node)
804     , m_oldValue(oldValue)
805     , m_newValue(newValue)
806 {
807 }
execute()808 void NodeModifyPriorityCmd::execute()
809 {
810     m_node.setPriority(m_newValue);
811 }
unexecute()812 void NodeModifyPriorityCmd::unexecute()
813 {
814     m_node.setPriority(m_oldValue);
815 }
816 
NodeModifyLeaderCmd(Node & node,const QString & leader,const KUndo2MagicString & name)817 NodeModifyLeaderCmd::NodeModifyLeaderCmd(Node &node, const QString& leader, const KUndo2MagicString& name)
818         : NamedCommand(name),
819         m_node(node),
820         newLeader(leader),
821         oldLeader(node.leader())
822 {
823 }
execute()824 void NodeModifyLeaderCmd::execute()
825 {
826     m_node.setLeader(newLeader);
827 
828 
829 }
unexecute()830 void NodeModifyLeaderCmd::unexecute()
831 {
832     m_node.setLeader(oldLeader);
833 
834 
835 }
836 
NodeModifyDescriptionCmd(Node & node,const QString & description,const KUndo2MagicString & name)837 NodeModifyDescriptionCmd::NodeModifyDescriptionCmd(Node &node, const QString& description, const KUndo2MagicString& name)
838         : NamedCommand(name),
839         m_node(node),
840         newDescription(description),
841         oldDescription(node.description())
842 {
843 }
execute()844 void NodeModifyDescriptionCmd::execute()
845 {
846     m_node.setDescription(newDescription);
847 
848 
849 }
unexecute()850 void NodeModifyDescriptionCmd::unexecute()
851 {
852     m_node.setDescription(oldDescription);
853 
854 
855 }
856 
NodeModifyConstraintCmd(Node & node,Node::ConstraintType c,const KUndo2MagicString & name)857 NodeModifyConstraintCmd::NodeModifyConstraintCmd(Node &node, Node::ConstraintType c, const KUndo2MagicString& name)
858         : NamedCommand(name),
859         m_node(node),
860         newConstraint(c),
861         oldConstraint(static_cast<Node::ConstraintType>(node.constraint()))
862 {
863 }
execute()864 void NodeModifyConstraintCmd::execute()
865 {
866     m_node.setConstraint(newConstraint);
867 }
unexecute()868 void NodeModifyConstraintCmd::unexecute()
869 {
870     m_node.setConstraint(oldConstraint);
871 }
872 
NodeModifyConstraintStartTimeCmd(Node & node,const QDateTime & dt,const KUndo2MagicString & name)873 NodeModifyConstraintStartTimeCmd::NodeModifyConstraintStartTimeCmd(Node &node, const QDateTime& dt, const KUndo2MagicString& name)
874         : NamedCommand(name),
875         m_node(node),
876         newTime(dt),
877         oldTime(node.constraintStartTime())
878 {
879     if (node.projectNode()) {
880         m_timeZone = static_cast<Project*>(node.projectNode())->timeZone();
881     }
882 }
execute()883 void NodeModifyConstraintStartTimeCmd::execute()
884 {
885     m_node.setConstraintStartTime(DateTime(newTime, m_timeZone));
886 
887 }
unexecute()888 void NodeModifyConstraintStartTimeCmd::unexecute()
889 {
890     m_node.setConstraintStartTime(oldTime);
891 
892 }
893 
NodeModifyConstraintEndTimeCmd(Node & node,const QDateTime & dt,const KUndo2MagicString & name)894 NodeModifyConstraintEndTimeCmd::NodeModifyConstraintEndTimeCmd(Node &node, const QDateTime& dt, const KUndo2MagicString& name)
895         : NamedCommand(name),
896         m_node(node),
897         newTime(dt),
898         oldTime(node.constraintEndTime())
899 {
900     if (node.projectNode()) {
901         m_timeZone = static_cast<Project*>(node.projectNode())->timeZone();
902     }
903 }
execute()904 void NodeModifyConstraintEndTimeCmd::execute()
905 {
906     m_node.setConstraintEndTime(DateTime(newTime, m_timeZone));
907 }
unexecute()908 void NodeModifyConstraintEndTimeCmd::unexecute()
909 {
910     m_node.setConstraintEndTime(oldTime);
911 }
912 
NodeModifyStartTimeCmd(Node & node,const QDateTime & dt,const KUndo2MagicString & name)913 NodeModifyStartTimeCmd::NodeModifyStartTimeCmd(Node &node, const QDateTime& dt, const KUndo2MagicString& name)
914         : NamedCommand(name),
915         m_node(node),
916         newTime(dt),
917         oldTime(node.startTime())
918 {
919     m_timeZone = static_cast<Project*>(node.projectNode())->timeZone();
920 }
execute()921 void NodeModifyStartTimeCmd::execute()
922 {
923     m_node.setStartTime(DateTime(newTime, m_timeZone));
924 
925 
926 }
unexecute()927 void NodeModifyStartTimeCmd::unexecute()
928 {
929     m_node.setStartTime(oldTime);
930 
931 
932 }
933 
NodeModifyEndTimeCmd(Node & node,const QDateTime & dt,const KUndo2MagicString & name)934 NodeModifyEndTimeCmd::NodeModifyEndTimeCmd(Node &node, const QDateTime& dt, const KUndo2MagicString& name)
935         : NamedCommand(name),
936         m_node(node),
937         newTime(dt),
938         oldTime(node.endTime())
939 {
940     m_timeZone = static_cast<Project*>(node.projectNode())->timeZone();
941 }
execute()942 void NodeModifyEndTimeCmd::execute()
943 {
944     m_node.setEndTime(DateTime(newTime, m_timeZone));
945 
946 
947 }
unexecute()948 void NodeModifyEndTimeCmd::unexecute()
949 {
950     m_node.setEndTime(oldTime);
951 
952 
953 }
954 
NodeModifyIdCmd(Node & node,const QString & id,const KUndo2MagicString & name)955 NodeModifyIdCmd::NodeModifyIdCmd(Node &node, const QString& id, const KUndo2MagicString& name)
956         : NamedCommand(name),
957         m_node(node),
958         newId(id),
959         oldId(node.id())
960 {
961 }
execute()962 void NodeModifyIdCmd::execute()
963 {
964     m_node.setId(newId);
965 
966 
967 }
unexecute()968 void NodeModifyIdCmd::unexecute()
969 {
970     m_node.setId(oldId);
971 
972 
973 }
974 
NodeIndentCmd(Node & node,const KUndo2MagicString & name)975 NodeIndentCmd::NodeIndentCmd(Node &node, const KUndo2MagicString& name)
976         : NamedCommand(name),
977         m_node(node),
978         m_newparent(0),
979         m_newindex(-1),
980         m_cmd(0)
981 {
982 }
~NodeIndentCmd()983 NodeIndentCmd::~NodeIndentCmd()
984 {
985     delete m_cmd;
986 }
execute()987 void NodeIndentCmd::execute()
988 {
989     m_oldparent = m_node.parentNode();
990     m_oldindex = m_oldparent->findChildNode(&m_node);
991     Project *p = dynamic_cast<Project *>(m_node.projectNode());
992     if (p && p->indentTask(&m_node, m_newindex)) {
993         m_newparent = m_node.parentNode();
994         m_newindex = m_newparent->findChildNode(&m_node);
995         // Summarytasks can't have resources, so remove resource requests from the new parent
996         if (m_cmd == 0) {
997             foreach (ResourceGroupRequest *r, m_newparent->requests().requests()) {
998                 if (m_cmd == 0) m_cmd = new MacroCommand(KUndo2MagicString());
999                 m_cmd->addCommand(new RemoveResourceGroupRequestCmd(r));
1000             }
1001             // Also remove accounts
1002             if (m_newparent->runningAccount()) {
1003                 if (m_cmd == 0) m_cmd = new MacroCommand(KUndo2MagicString());
1004                 m_cmd->addCommand(new NodeModifyRunningAccountCmd(*m_newparent, m_newparent->runningAccount(), 0));
1005             }
1006             if (m_newparent->startupAccount()) {
1007                 if (m_cmd == 0) m_cmd = new MacroCommand(KUndo2MagicString());
1008                 m_cmd->addCommand(new NodeModifyStartupAccountCmd(*m_newparent, m_newparent->startupAccount(), 0));
1009             }
1010             if (m_newparent->shutdownAccount()) {
1011                 if (m_cmd == 0) m_cmd = new MacroCommand(KUndo2MagicString());
1012                 m_cmd->addCommand(new NodeModifyShutdownAccountCmd(*m_newparent, m_newparent->shutdownAccount(), 0));
1013             }
1014        }
1015         if (m_cmd) {
1016             m_cmd->execute();
1017         }
1018     }
1019 }
unexecute()1020 void NodeIndentCmd::unexecute()
1021 {
1022     Project * p = dynamic_cast<Project *>(m_node.projectNode());
1023     if (m_newindex != -1 && p && p->unindentTask(&m_node)) {
1024         m_newindex = -1;
1025         if (m_cmd) {
1026             m_cmd->unexecute();
1027         }
1028     }
1029 
1030 
1031 }
1032 
NodeUnindentCmd(Node & node,const KUndo2MagicString & name)1033 NodeUnindentCmd::NodeUnindentCmd(Node &node, const KUndo2MagicString& name)
1034         : NamedCommand(name),
1035         m_node(node),
1036         m_newparent(0),
1037         m_newindex(-1)
1038 {}
execute()1039 void NodeUnindentCmd::execute()
1040 {
1041     m_oldparent = m_node.parentNode();
1042     m_oldindex = m_oldparent->findChildNode(&m_node);
1043     Project *p = dynamic_cast<Project *>(m_node.projectNode());
1044     if (p && p->unindentTask(&m_node)) {
1045         m_newparent = m_node.parentNode();
1046         m_newindex = m_newparent->findChildNode(&m_node);
1047     }
1048 
1049 
1050 }
unexecute()1051 void NodeUnindentCmd::unexecute()
1052 {
1053     Project * p = dynamic_cast<Project *>(m_node.projectNode());
1054     if (m_newindex != -1 && p && p->indentTask(&m_node, m_oldindex)) {
1055         m_newindex = -1;
1056     }
1057 
1058 
1059 }
1060 
NodeMoveUpCmd(Node & node,const KUndo2MagicString & name)1061 NodeMoveUpCmd::NodeMoveUpCmd(Node &node, const KUndo2MagicString& name)
1062         : NamedCommand(name),
1063         m_node(node),
1064         m_moved(false)
1065 {
1066 
1067     m_project = static_cast<Project *>(m_node.projectNode());
1068 }
execute()1069 void NodeMoveUpCmd::execute()
1070 {
1071     if (m_project) {
1072         m_moved = m_project->moveTaskUp(&m_node);
1073     }
1074 
1075 
1076 }
unexecute()1077 void NodeMoveUpCmd::unexecute()
1078 {
1079     if (m_project && m_moved) {
1080         m_project->moveTaskDown(&m_node);
1081     }
1082     m_moved = false;
1083 
1084 }
1085 
NodeMoveDownCmd(Node & node,const KUndo2MagicString & name)1086 NodeMoveDownCmd::NodeMoveDownCmd(Node &node, const KUndo2MagicString& name)
1087         : NamedCommand(name),
1088         m_node(node),
1089         m_moved(false)
1090 {
1091 
1092     m_project = static_cast<Project *>(m_node.projectNode());
1093 }
execute()1094 void NodeMoveDownCmd::execute()
1095 {
1096     if (m_project) {
1097         m_moved = m_project->moveTaskDown(&m_node);
1098     }
1099 
1100 }
unexecute()1101 void NodeMoveDownCmd::unexecute()
1102 {
1103     if (m_project && m_moved) {
1104         m_project->moveTaskUp(&m_node);
1105     }
1106     m_moved = false;
1107 
1108 }
1109 
NodeMoveCmd(Project * project,Node * node,Node * newParent,int newPos,const KUndo2MagicString & name)1110 NodeMoveCmd::NodeMoveCmd(Project *project, Node *node, Node *newParent, int newPos, const KUndo2MagicString& name)
1111     : NamedCommand(name),
1112     m_project(project),
1113     m_node(node),
1114     m_newparent(newParent),
1115     m_newpos(newPos),
1116     m_moved(false)
1117 {
1118     m_oldparent = node->parentNode();
1119     Q_ASSERT(m_oldparent);
1120 }
execute()1121 void NodeMoveCmd::execute()
1122 {
1123     if (m_project) {
1124         m_oldpos = m_oldparent->indexOf(m_node);
1125         m_moved = m_project->moveTask(m_node, m_newparent, m_newpos);
1126         if (m_moved) {
1127             if (m_cmd.isEmpty()) {
1128                 // Summarytasks can't have resources, so remove resource requests from the new parent
1129                 foreach (ResourceGroupRequest *r, m_newparent->requests().requests()) {
1130                     m_cmd.addCommand(new RemoveResourceGroupRequestCmd(r));
1131                 }
1132                 // TODO appointments ??
1133             }
1134             m_cmd.execute();
1135         }
1136     }
1137 }
unexecute()1138 void NodeMoveCmd::unexecute()
1139 {
1140     if (m_project && m_moved) {
1141         m_moved = m_project->moveTask(m_node, m_oldparent, m_oldpos);
1142         m_cmd.unexecute();
1143     }
1144     m_moved = false;
1145 }
1146 
AddRelationCmd(Project & project,Relation * rel,const KUndo2MagicString & name)1147 AddRelationCmd::AddRelationCmd(Project &project, Relation *rel, const KUndo2MagicString& name)
1148         : NamedCommand(name),
1149         m_rel(rel),
1150         m_project(project)
1151 {
1152     m_taken = true;
1153 }
~AddRelationCmd()1154 AddRelationCmd::~AddRelationCmd()
1155 {
1156     if (m_taken)
1157         delete m_rel;
1158 }
execute()1159 void AddRelationCmd::execute()
1160 {
1161     //debugPlan<<m_rel->parent()<<" to"<<m_rel->child();
1162     m_taken = false;
1163     m_project.addRelation(m_rel, false);
1164 }
unexecute()1165 void AddRelationCmd::unexecute()
1166 {
1167     m_taken = true;
1168     m_project.takeRelation(m_rel);
1169 }
1170 
DeleteRelationCmd(Project & project,Relation * rel,const KUndo2MagicString & name)1171 DeleteRelationCmd::DeleteRelationCmd(Project &project, Relation *rel, const KUndo2MagicString& name)
1172         : NamedCommand(name),
1173         m_rel(rel),
1174         m_project(project)
1175 {
1176     m_taken = false;
1177 }
~DeleteRelationCmd()1178 DeleteRelationCmd::~DeleteRelationCmd()
1179 {
1180     if (m_taken) {
1181         // do not access nodes, the may already be deleted
1182         m_rel->setParent(0);
1183         m_rel->setChild(0);
1184         delete m_rel;
1185     }
1186 }
execute()1187 void DeleteRelationCmd::execute()
1188 {
1189     //debugPlan<<m_rel->parent()<<" to"<<m_rel->child();
1190     m_taken = true;
1191     m_project.takeRelation(m_rel);
1192 }
unexecute()1193 void DeleteRelationCmd::unexecute()
1194 {
1195     m_taken = false;
1196     m_project.addRelation(m_rel, false);
1197 }
1198 
ModifyRelationTypeCmd(Relation * rel,Relation::Type type,const KUndo2MagicString & name)1199 ModifyRelationTypeCmd::ModifyRelationTypeCmd(Relation *rel, Relation::Type type, const KUndo2MagicString& name)
1200         : NamedCommand(name),
1201         m_rel(rel),
1202         m_newtype(type)
1203 {
1204 
1205     m_oldtype = rel->type();
1206     m_project = dynamic_cast<Project*>(rel->parent() ->projectNode());
1207 }
execute()1208 void ModifyRelationTypeCmd::execute()
1209 {
1210     if (m_project) {
1211         m_project->setRelationType(m_rel, m_newtype);
1212     }
1213 }
unexecute()1214 void ModifyRelationTypeCmd::unexecute()
1215 {
1216     if (m_project) {
1217         m_project->setRelationType(m_rel, m_oldtype);
1218     }
1219 }
1220 
ModifyRelationLagCmd(Relation * rel,Duration lag,const KUndo2MagicString & name)1221 ModifyRelationLagCmd::ModifyRelationLagCmd(Relation *rel, Duration lag, const KUndo2MagicString& name)
1222         : NamedCommand(name),
1223         m_rel(rel),
1224         m_newlag(lag)
1225 {
1226 
1227     m_oldlag = rel->lag();
1228     m_project = dynamic_cast<Project*>(rel->parent() ->projectNode());
1229 }
execute()1230 void ModifyRelationLagCmd::execute()
1231 {
1232     if (m_project) {
1233         m_project->setRelationLag(m_rel, m_newlag);
1234     }
1235 }
unexecute()1236 void ModifyRelationLagCmd::unexecute()
1237 {
1238     if (m_project) {
1239         m_project->setRelationLag(m_rel, m_oldlag);
1240     }
1241 }
1242 
AddResourceRequestCmd(ResourceGroupRequest * group,ResourceRequest * request,const KUndo2MagicString & name)1243 AddResourceRequestCmd::AddResourceRequestCmd(ResourceGroupRequest *group, ResourceRequest *request, const KUndo2MagicString& name)
1244         : NamedCommand(name),
1245         m_group(group),
1246         m_request(request)
1247 {
1248 
1249     m_mine = true;
1250 }
~AddResourceRequestCmd()1251 AddResourceRequestCmd::~AddResourceRequestCmd()
1252 {
1253     if (m_mine)
1254         delete m_request;
1255 }
execute()1256 void AddResourceRequestCmd::execute()
1257 {
1258     //debugPlan<<"group="<<m_group<<" req="<<m_request;
1259     m_group->addResourceRequest(m_request);
1260     m_mine = false;
1261 }
unexecute()1262 void AddResourceRequestCmd::unexecute()
1263 {
1264     //debugPlan<<"group="<<m_group<<" req="<<m_request;
1265     m_group->takeResourceRequest(m_request);
1266     m_mine = true;
1267 }
1268 
RemoveResourceRequestCmd(ResourceGroupRequest * group,ResourceRequest * request,const KUndo2MagicString & name)1269 RemoveResourceRequestCmd::RemoveResourceRequestCmd(ResourceGroupRequest *group, ResourceRequest *request, const KUndo2MagicString& name)
1270         : NamedCommand(name),
1271         m_group(group),
1272         m_request(request)
1273 {
1274 
1275     m_mine = false;
1276     //debugPlan<<"group req="<<group<<" req="<<request<<" to gr="<<m_group->group();
1277 }
~RemoveResourceRequestCmd()1278 RemoveResourceRequestCmd::~RemoveResourceRequestCmd()
1279 {
1280     if (m_mine)
1281         delete m_request;
1282 }
execute()1283 void RemoveResourceRequestCmd::execute()
1284 {
1285     m_group->takeResourceRequest(m_request);
1286     m_mine = true;
1287 }
unexecute()1288 void RemoveResourceRequestCmd::unexecute()
1289 {
1290     m_group->addResourceRequest(m_request);
1291     m_mine = false;
1292 }
1293 
ModifyResourceRequestUnitsCmd(ResourceRequest * request,int oldvalue,int newvalue,const KUndo2MagicString & name)1294 ModifyResourceRequestUnitsCmd::ModifyResourceRequestUnitsCmd(ResourceRequest *request, int oldvalue, int newvalue, const KUndo2MagicString& name)
1295     : NamedCommand(name),
1296     m_request(request),
1297     m_oldvalue(oldvalue),
1298     m_newvalue(newvalue)
1299 {
1300 }
execute()1301 void ModifyResourceRequestUnitsCmd::execute()
1302 {
1303     m_request->setUnits(m_newvalue);
1304 }
unexecute()1305 void ModifyResourceRequestUnitsCmd::unexecute()
1306 {
1307     m_request->setUnits(m_oldvalue);
1308 }
1309 
ModifyResourceRequestRequiredCmd(ResourceRequest * request,const QList<Resource * > & value,const KUndo2MagicString & name)1310 ModifyResourceRequestRequiredCmd::ModifyResourceRequestRequiredCmd(ResourceRequest *request, const QList<Resource*> &value, const KUndo2MagicString& name)
1311     : NamedCommand(name),
1312     m_request(request),
1313     m_newvalue(value)
1314 {
1315     m_oldvalue = request->requiredResources();
1316 }
execute()1317 void ModifyResourceRequestRequiredCmd::execute()
1318 {
1319     m_request->setRequiredResources(m_newvalue);
1320 }
unexecute()1321 void ModifyResourceRequestRequiredCmd::unexecute()
1322 {
1323     m_request->setRequiredResources(m_oldvalue);
1324 }
1325 
ModifyResourceGroupRequestUnitsCmd(ResourceGroupRequest * request,int oldvalue,int newvalue,const KUndo2MagicString & name)1326 ModifyResourceGroupRequestUnitsCmd::ModifyResourceGroupRequestUnitsCmd(ResourceGroupRequest *request, int oldvalue, int newvalue, const KUndo2MagicString& name)
1327     : NamedCommand(name),
1328     m_request(request),
1329     m_oldvalue(oldvalue),
1330     m_newvalue(newvalue)
1331 {
1332 }
execute()1333 void ModifyResourceGroupRequestUnitsCmd::execute()
1334 {
1335     m_request->setUnits(m_newvalue);
1336 }
unexecute()1337 void ModifyResourceGroupRequestUnitsCmd::unexecute()
1338 {
1339     m_request->setUnits(m_oldvalue);
1340 }
1341 
1342 
ModifyEstimateCmd(Node & node,double oldvalue,double newvalue,const KUndo2MagicString & name)1343 ModifyEstimateCmd::ModifyEstimateCmd(Node &node, double oldvalue, double newvalue, const KUndo2MagicString& name)
1344     : NamedCommand(name),
1345     m_estimate(node.estimate()),
1346     m_oldvalue(oldvalue),
1347     m_newvalue(newvalue),
1348     m_optimistic(node.estimate()->optimisticRatio()),
1349     m_pessimistic(node.estimate()->pessimisticRatio()),
1350     m_cmd(0)
1351 {
1352     if (newvalue == 0.0) {
1353         // Milestones can't have resources, so remove resource requests
1354         foreach (ResourceGroupRequest *r, node.requests().requests()) {
1355             if (m_cmd == 0) m_cmd = new MacroCommand(KUndo2MagicString());
1356             m_cmd->addCommand(new RemoveResourceGroupRequestCmd(r));
1357         }
1358     }
1359 }
~ModifyEstimateCmd()1360 ModifyEstimateCmd::~ModifyEstimateCmd()
1361 {
1362     delete m_cmd;
1363 }
execute()1364 void ModifyEstimateCmd::execute()
1365 {
1366     m_estimate->setExpectedEstimate(m_newvalue);
1367     if (m_cmd) {
1368         m_cmd->execute();
1369     }
1370     m_estimate->setPessimisticRatio(m_pessimistic);
1371     m_estimate->setOptimisticRatio(m_optimistic);
1372 }
unexecute()1373 void ModifyEstimateCmd::unexecute()
1374 {
1375     m_estimate->setExpectedEstimate(m_oldvalue);
1376     if (m_cmd) {
1377         m_cmd->unexecute();
1378     }
1379     m_estimate->setPessimisticRatio(m_pessimistic);
1380     m_estimate->setOptimisticRatio(m_optimistic);
1381 }
1382 
EstimateModifyOptimisticRatioCmd(Node & node,int oldvalue,int newvalue,const KUndo2MagicString & name)1383 EstimateModifyOptimisticRatioCmd::EstimateModifyOptimisticRatioCmd(Node &node, int oldvalue, int newvalue, const KUndo2MagicString& name)
1384         : NamedCommand(name),
1385         m_estimate(node.estimate()),
1386         m_oldvalue(oldvalue),
1387         m_newvalue(newvalue)
1388 {
1389 }
execute()1390 void EstimateModifyOptimisticRatioCmd::execute()
1391 {
1392     m_estimate->setOptimisticRatio(m_newvalue);
1393 }
unexecute()1394 void EstimateModifyOptimisticRatioCmd::unexecute()
1395 {
1396     m_estimate->setOptimisticRatio(m_oldvalue);
1397 }
1398 
EstimateModifyPessimisticRatioCmd(Node & node,int oldvalue,int newvalue,const KUndo2MagicString & name)1399 EstimateModifyPessimisticRatioCmd::EstimateModifyPessimisticRatioCmd(Node &node, int oldvalue, int newvalue, const KUndo2MagicString& name)
1400         : NamedCommand(name),
1401         m_estimate(node.estimate()),
1402         m_oldvalue(oldvalue),
1403         m_newvalue(newvalue)
1404 {
1405 }
execute()1406 void EstimateModifyPessimisticRatioCmd::execute()
1407 {
1408     m_estimate->setPessimisticRatio(m_newvalue);
1409 }
unexecute()1410 void EstimateModifyPessimisticRatioCmd::unexecute()
1411 {
1412     m_estimate->setPessimisticRatio(m_oldvalue);
1413 }
1414 
ModifyEstimateTypeCmd(Node & node,int oldvalue,int newvalue,const KUndo2MagicString & name)1415 ModifyEstimateTypeCmd::ModifyEstimateTypeCmd(Node &node, int oldvalue, int newvalue, const KUndo2MagicString& name)
1416         : NamedCommand(name),
1417         m_estimate(node.estimate()),
1418         m_oldvalue(oldvalue),
1419         m_newvalue(newvalue)
1420 {
1421 }
execute()1422 void ModifyEstimateTypeCmd::execute()
1423 {
1424     m_estimate->setType(static_cast<Estimate::Type>(m_newvalue));
1425 }
unexecute()1426 void ModifyEstimateTypeCmd::unexecute()
1427 {
1428     m_estimate->setType(static_cast<Estimate::Type>(m_oldvalue));
1429 }
1430 
ModifyEstimateCalendarCmd(Node & node,Calendar * oldvalue,Calendar * newvalue,const KUndo2MagicString & name)1431 ModifyEstimateCalendarCmd::ModifyEstimateCalendarCmd(Node &node, Calendar *oldvalue, Calendar *newvalue, const KUndo2MagicString& name)
1432     : NamedCommand(name),
1433         m_estimate(node.estimate()),
1434         m_oldvalue(oldvalue),
1435         m_newvalue(newvalue)
1436 {
1437 }
execute()1438 void ModifyEstimateCalendarCmd::execute()
1439 {
1440     m_estimate->setCalendar(m_newvalue);
1441 }
unexecute()1442 void ModifyEstimateCalendarCmd::unexecute()
1443 {
1444     m_estimate->setCalendar(m_oldvalue);
1445 }
1446 
ModifyEstimateUnitCmd(Node & node,Duration::Unit oldvalue,Duration::Unit newvalue,const KUndo2MagicString & name)1447 ModifyEstimateUnitCmd::ModifyEstimateUnitCmd(Node &node, Duration::Unit oldvalue, Duration::Unit newvalue, const KUndo2MagicString& name)
1448         : NamedCommand(name),
1449         m_estimate(node.estimate()),
1450         m_oldvalue(oldvalue),
1451         m_newvalue(newvalue)
1452 {
1453 }
execute()1454 void ModifyEstimateUnitCmd::execute()
1455 {
1456     m_estimate->setUnit(m_newvalue);
1457 
1458 }
unexecute()1459 void ModifyEstimateUnitCmd::unexecute()
1460 {
1461     m_estimate->setUnit(m_oldvalue);
1462 }
1463 
EstimateModifyRiskCmd(Node & node,int oldvalue,int newvalue,const KUndo2MagicString & name)1464 EstimateModifyRiskCmd::EstimateModifyRiskCmd(Node &node, int oldvalue, int newvalue, const KUndo2MagicString& name)
1465         : NamedCommand(name),
1466         m_estimate(node.estimate()),
1467         m_oldvalue(oldvalue),
1468         m_newvalue(newvalue)
1469 {
1470 }
execute()1471 void EstimateModifyRiskCmd::execute()
1472 {
1473     m_estimate->setRisktype(static_cast<Estimate::Risktype>(m_newvalue));
1474 }
unexecute()1475 void EstimateModifyRiskCmd::unexecute()
1476 {
1477     m_estimate->setRisktype(static_cast<Estimate::Risktype>(m_oldvalue));
1478 }
1479 
AddResourceGroupRequestCmd(Task & task,ResourceGroupRequest * request,const KUndo2MagicString & name)1480 AddResourceGroupRequestCmd::AddResourceGroupRequestCmd(Task &task, ResourceGroupRequest *request, const KUndo2MagicString& name)
1481         : NamedCommand(name),
1482         m_task(task),
1483         m_request(request)
1484 {
1485     m_mine = true;
1486 }
execute()1487 void AddResourceGroupRequestCmd::execute()
1488 {
1489     //debugPlan<<"group="<<m_request;
1490     m_task.addRequest(m_request);
1491     m_mine = false;
1492 
1493 
1494 }
unexecute()1495 void AddResourceGroupRequestCmd::unexecute()
1496 {
1497     //debugPlan<<"group="<<m_request;
1498     m_task.takeRequest(m_request); // group should now be empty of resourceRequests
1499     m_mine = true;
1500 
1501 
1502 }
1503 
RemoveResourceGroupRequestCmd(ResourceGroupRequest * request,const KUndo2MagicString & name)1504 RemoveResourceGroupRequestCmd::RemoveResourceGroupRequestCmd(ResourceGroupRequest *request, const KUndo2MagicString& name)
1505         : NamedCommand(name),
1506         m_task(*(request->parent() ->task())),
1507         m_request(request)
1508 {
1509 
1510     m_mine = false;
1511 }
1512 
RemoveResourceGroupRequestCmd(Task & task,ResourceGroupRequest * request,const KUndo2MagicString & name)1513 RemoveResourceGroupRequestCmd::RemoveResourceGroupRequestCmd(Task &task, ResourceGroupRequest *request, const KUndo2MagicString& name)
1514         : NamedCommand(name),
1515         m_task(task),
1516         m_request(request)
1517 {
1518 
1519     m_mine = false;
1520 }
execute()1521 void RemoveResourceGroupRequestCmd::execute()
1522 {
1523     //debugPlan<<"group="<<m_request;
1524     m_task.takeRequest(m_request); // group should now be empty of resourceRequests
1525     m_mine = true;
1526 
1527 
1528 }
unexecute()1529 void RemoveResourceGroupRequestCmd::unexecute()
1530 {
1531     //debugPlan<<"group="<<m_request;
1532     m_task.addRequest(m_request);
1533     m_mine = false;
1534 
1535 
1536 }
1537 
AddResourceCmd(ResourceGroup * group,Resource * resource,const KUndo2MagicString & name)1538 AddResourceCmd::AddResourceCmd(ResourceGroup *group, Resource *resource, const KUndo2MagicString& name)
1539         : NamedCommand(name),
1540         m_group(group),
1541         m_resource(resource)
1542 {
1543     m_index = group->indexOf(resource);
1544     m_mine = true;
1545 }
~AddResourceCmd()1546 AddResourceCmd::~AddResourceCmd()
1547 {
1548     if (m_mine) {
1549         //debugPlan<<"delete:"<<m_resource;
1550         delete m_resource;
1551     }
1552 }
execute()1553 void AddResourceCmd::execute()
1554 {
1555     Q_ASSERT(m_group->project());
1556     if (m_group->project()) {
1557         m_group->project()->addResource(m_group, m_resource, m_index);
1558         m_mine = false;
1559         //debugPlan<<"added:"<<m_resource;
1560     }
1561 
1562 }
unexecute()1563 void AddResourceCmd::unexecute()
1564 {
1565     Q_ASSERT(m_group->project());
1566     if (m_group->project()) {
1567         m_group->project()->takeResource(m_group, m_resource);
1568         //debugPlan<<"removed:"<<m_resource;
1569         m_mine = true;
1570     }
1571 
1572     Q_ASSERT(m_group->project());
1573 }
1574 
RemoveResourceCmd(ResourceGroup * group,Resource * resource,const KUndo2MagicString & name)1575 RemoveResourceCmd::RemoveResourceCmd(ResourceGroup *group, Resource *resource, const KUndo2MagicString& name)
1576         : AddResourceCmd(group, resource, name)
1577 {
1578     //debugPlan<<resource;
1579     m_mine = false;
1580     m_requests = m_resource->requests();
1581 
1582     if (group->project()) {
1583         foreach (Schedule * s, group->project()->schedules()) {
1584             Schedule *rs = resource->findSchedule(s->id());
1585             if (rs && ! rs->isDeleted()) {
1586                 debugPlan<<s->name();
1587                 addSchScheduled(s);
1588             }
1589         }
1590     }
1591     if (resource->account()) {
1592         m_cmd.addCommand(new ResourceModifyAccountCmd(*resource, resource->account(), 0));
1593     }
1594 }
~RemoveResourceCmd()1595 RemoveResourceCmd::~RemoveResourceCmd()
1596 {
1597     while (!m_appointments.isEmpty())
1598         delete m_appointments.takeFirst();
1599 }
execute()1600 void RemoveResourceCmd::execute()
1601 {
1602     foreach (ResourceRequest * r, m_requests) {
1603         r->parent() ->takeResourceRequest(r);
1604         //debugPlan<<"Remove request for"<<r->resource()->name();
1605     }
1606     AddResourceCmd::unexecute();
1607     m_cmd.execute();
1608     setSchScheduled(false);
1609 }
unexecute()1610 void RemoveResourceCmd::unexecute()
1611 {
1612     foreach (ResourceRequest * r, m_requests) {
1613         r->parent() ->addResourceRequest(r);
1614         //debugPlan<<"Add request for"<<r->resource()->name();
1615     }
1616     m_cmd.unexecute();
1617     AddResourceCmd::execute();
1618     setSchScheduled();
1619 }
1620 
MoveResourceCmd(ResourceGroup * group,Resource * resource,const KUndo2MagicString & name)1621 MoveResourceCmd::MoveResourceCmd(ResourceGroup *group, Resource *resource, const KUndo2MagicString& name)
1622     : NamedCommand(name),
1623     m_project(*(group->project())),
1624     m_resource(resource),
1625     m_oldvalue(resource->parentGroup()),
1626     m_newvalue(group)
1627 {
1628     foreach (ResourceRequest * r, resource->requests()) {
1629         cmd.addCommand(new RemoveResourceRequestCmd(r->parent(), r));
1630     }
1631 }
execute()1632 void MoveResourceCmd::execute()
1633 {
1634     cmd.execute();
1635     m_project.moveResource(m_newvalue, m_resource);
1636 }
unexecute()1637 void MoveResourceCmd::unexecute()
1638 {
1639     m_project.moveResource(m_oldvalue, m_resource);
1640     cmd.unexecute();
1641 }
1642 
ModifyResourceNameCmd(Resource * resource,const QString & value,const KUndo2MagicString & name)1643 ModifyResourceNameCmd::ModifyResourceNameCmd(Resource *resource, const QString& value, const KUndo2MagicString& name)
1644         : NamedCommand(name),
1645         m_resource(resource),
1646         m_newvalue(value)
1647 {
1648     m_oldvalue = resource->name();
1649 }
execute()1650 void ModifyResourceNameCmd::execute()
1651 {
1652     m_resource->setName(m_newvalue);
1653 
1654 
1655 }
unexecute()1656 void ModifyResourceNameCmd::unexecute()
1657 {
1658     m_resource->setName(m_oldvalue);
1659 }
ModifyResourceInitialsCmd(Resource * resource,const QString & value,const KUndo2MagicString & name)1660 ModifyResourceInitialsCmd::ModifyResourceInitialsCmd(Resource *resource, const QString& value, const KUndo2MagicString& name)
1661         : NamedCommand(name),
1662         m_resource(resource),
1663         m_newvalue(value)
1664 {
1665     m_oldvalue = resource->initials();
1666 }
execute()1667 void ModifyResourceInitialsCmd::execute()
1668 {
1669     m_resource->setInitials(m_newvalue);
1670 
1671 
1672 }
unexecute()1673 void ModifyResourceInitialsCmd::unexecute()
1674 {
1675     m_resource->setInitials(m_oldvalue);
1676 
1677 
1678 }
ModifyResourceEmailCmd(Resource * resource,const QString & value,const KUndo2MagicString & name)1679 ModifyResourceEmailCmd::ModifyResourceEmailCmd(Resource *resource, const QString& value, const KUndo2MagicString& name)
1680         : NamedCommand(name),
1681         m_resource(resource),
1682         m_newvalue(value)
1683 {
1684     m_oldvalue = resource->email();
1685 }
execute()1686 void ModifyResourceEmailCmd::execute()
1687 {
1688     m_resource->setEmail(m_newvalue);
1689 }
unexecute()1690 void ModifyResourceEmailCmd::unexecute()
1691 {
1692     m_resource->setEmail(m_oldvalue);
1693 }
ModifyResourceAutoAllocateCmd(Resource * resource,bool value,const KUndo2MagicString & name)1694 ModifyResourceAutoAllocateCmd::ModifyResourceAutoAllocateCmd(Resource *resource,bool value, const KUndo2MagicString& name)
1695     : NamedCommand(name),
1696     m_resource(resource),
1697     m_newvalue(value)
1698 {
1699     m_oldvalue = resource->autoAllocate();
1700 }
execute()1701 void ModifyResourceAutoAllocateCmd::execute()
1702 {
1703     m_resource->setAutoAllocate(m_newvalue);
1704 }
unexecute()1705 void ModifyResourceAutoAllocateCmd::unexecute()
1706 {
1707     m_resource->setAutoAllocate(m_oldvalue);
1708 }
ModifyResourceTypeCmd(Resource * resource,int value,const KUndo2MagicString & name)1709 ModifyResourceTypeCmd::ModifyResourceTypeCmd(Resource *resource, int value, const KUndo2MagicString& name)
1710         : NamedCommand(name),
1711         m_resource(resource),
1712         m_newvalue(value)
1713 {
1714     m_oldvalue = resource->type();
1715 }
execute()1716 void ModifyResourceTypeCmd::execute()
1717 {
1718     m_resource->setType((Resource::Type) m_newvalue);
1719 }
unexecute()1720 void ModifyResourceTypeCmd::unexecute()
1721 {
1722     m_resource->setType((Resource::Type) m_oldvalue);
1723 }
ModifyResourceUnitsCmd(Resource * resource,int value,const KUndo2MagicString & name)1724 ModifyResourceUnitsCmd::ModifyResourceUnitsCmd(Resource *resource, int value, const KUndo2MagicString& name)
1725         : NamedCommand(name),
1726         m_resource(resource),
1727         m_newvalue(value)
1728 {
1729     m_oldvalue = resource->units();
1730 }
execute()1731 void ModifyResourceUnitsCmd::execute()
1732 {
1733     m_resource->setUnits(m_newvalue);
1734 }
unexecute()1735 void ModifyResourceUnitsCmd::unexecute()
1736 {
1737     m_resource->setUnits(m_oldvalue);
1738 }
1739 
ModifyResourceAvailableFromCmd(Resource * resource,const QDateTime & value,const KUndo2MagicString & name)1740 ModifyResourceAvailableFromCmd::ModifyResourceAvailableFromCmd(Resource *resource, const QDateTime& value, const KUndo2MagicString& name)
1741         : NamedCommand(name),
1742         m_resource(resource),
1743         m_newvalue(value)
1744 {
1745     m_oldvalue = resource->availableFrom();
1746     m_timeZone = resource->timeZone();
1747 }
execute()1748 void ModifyResourceAvailableFromCmd::execute()
1749 {
1750     m_resource->setAvailableFrom(DateTime(m_newvalue, m_timeZone));
1751 }
unexecute()1752 void ModifyResourceAvailableFromCmd::unexecute()
1753 {
1754     m_resource->setAvailableFrom(m_oldvalue);
1755 }
1756 
ModifyResourceAvailableUntilCmd(Resource * resource,const QDateTime & value,const KUndo2MagicString & name)1757 ModifyResourceAvailableUntilCmd::ModifyResourceAvailableUntilCmd(Resource *resource, const QDateTime& value, const KUndo2MagicString& name)
1758         : NamedCommand(name),
1759         m_resource(resource),
1760         m_newvalue(value)
1761 {
1762     m_oldvalue = resource->availableUntil();
1763     m_timeZone = resource->timeZone();
1764 }
execute()1765 void ModifyResourceAvailableUntilCmd::execute()
1766 {
1767     m_resource->setAvailableUntil(DateTime(m_newvalue, m_timeZone));
1768 }
unexecute()1769 void ModifyResourceAvailableUntilCmd::unexecute()
1770 {
1771     m_resource->setAvailableUntil(m_oldvalue);
1772 }
1773 
ModifyResourceNormalRateCmd(Resource * resource,double value,const KUndo2MagicString & name)1774 ModifyResourceNormalRateCmd::ModifyResourceNormalRateCmd(Resource *resource, double value, const KUndo2MagicString& name)
1775         : NamedCommand(name),
1776         m_resource(resource),
1777         m_newvalue(value)
1778 {
1779     m_oldvalue = resource->normalRate();
1780 }
execute()1781 void ModifyResourceNormalRateCmd::execute()
1782 {
1783     m_resource->setNormalRate(m_newvalue);
1784 
1785 
1786 }
unexecute()1787 void ModifyResourceNormalRateCmd::unexecute()
1788 {
1789     m_resource->setNormalRate(m_oldvalue);
1790 
1791 
1792 }
ModifyResourceOvertimeRateCmd(Resource * resource,double value,const KUndo2MagicString & name)1793 ModifyResourceOvertimeRateCmd::ModifyResourceOvertimeRateCmd(Resource *resource, double value, const KUndo2MagicString& name)
1794         : NamedCommand(name),
1795         m_resource(resource),
1796         m_newvalue(value)
1797 {
1798     m_oldvalue = resource->overtimeRate();
1799 }
execute()1800 void ModifyResourceOvertimeRateCmd::execute()
1801 {
1802     m_resource->setOvertimeRate(m_newvalue);
1803 
1804 
1805 }
unexecute()1806 void ModifyResourceOvertimeRateCmd::unexecute()
1807 {
1808     m_resource->setOvertimeRate(m_oldvalue);
1809 
1810 
1811 }
1812 
ModifyResourceCalendarCmd(Resource * resource,Calendar * value,const KUndo2MagicString & name)1813 ModifyResourceCalendarCmd::ModifyResourceCalendarCmd(Resource *resource, Calendar *value, const KUndo2MagicString& name)
1814         : NamedCommand(name),
1815         m_resource(resource),
1816         m_newvalue(value)
1817 {
1818     m_oldvalue = resource->calendar(true);
1819 }
execute()1820 void ModifyResourceCalendarCmd::execute()
1821 {
1822     m_resource->setCalendar(m_newvalue);
1823 }
unexecute()1824 void ModifyResourceCalendarCmd::unexecute()
1825 {
1826     m_resource->setCalendar(m_oldvalue);
1827 }
1828 
ModifyRequiredResourcesCmd(Resource * resource,const QStringList & value,const KUndo2MagicString & name)1829 ModifyRequiredResourcesCmd::ModifyRequiredResourcesCmd(Resource *resource, const QStringList &value, const KUndo2MagicString& name)
1830         : NamedCommand(name),
1831         m_resource(resource),
1832         m_newvalue(value)
1833 {
1834     m_oldvalue = resource->requiredIds();
1835 }
execute()1836 void ModifyRequiredResourcesCmd::execute()
1837 {
1838     m_resource->setRequiredIds(m_newvalue);
1839 }
unexecute()1840 void ModifyRequiredResourcesCmd::unexecute()
1841 {
1842     m_resource->setRequiredIds(m_oldvalue);
1843 }
1844 
AddResourceTeamCmd(Resource * team,const QString & member,const KUndo2MagicString & name)1845 AddResourceTeamCmd::AddResourceTeamCmd(Resource *team, const QString &member, const KUndo2MagicString& name)
1846     : NamedCommand(name),
1847     m_team(team),
1848     m_member(member)
1849 {
1850 }
execute()1851 void AddResourceTeamCmd::execute()
1852 {
1853     m_team->addTeamMemberId(m_member);
1854 }
unexecute()1855 void AddResourceTeamCmd::unexecute()
1856 {
1857     m_team->removeTeamMemberId(m_member);
1858 }
1859 
RemoveResourceTeamCmd(Resource * team,const QString & member,const KUndo2MagicString & name)1860 RemoveResourceTeamCmd::RemoveResourceTeamCmd(Resource *team, const QString &member, const KUndo2MagicString& name)
1861     : NamedCommand(name),
1862     m_team(team),
1863     m_member(member)
1864 {
1865 }
execute()1866 void RemoveResourceTeamCmd::execute()
1867 {
1868     m_team->removeTeamMemberId(m_member);
1869 }
unexecute()1870 void RemoveResourceTeamCmd::unexecute()
1871 {
1872     m_team->addTeamMemberId(m_member);
1873 }
1874 
RemoveResourceGroupCmd(Project * project,ResourceGroup * group,const KUndo2MagicString & name)1875 RemoveResourceGroupCmd::RemoveResourceGroupCmd(Project *project, ResourceGroup *group, const KUndo2MagicString& name)
1876         : NamedCommand(name),
1877         m_group(group),
1878         m_project(project),
1879         m_cmd(0)
1880 {
1881     m_index = project->indexOf(group);
1882     m_mine = false;
1883     if (!m_group->requests().isEmpty()) {
1884         m_cmd = new MacroCommand(KUndo2MagicString());
1885         foreach(ResourceGroupRequest * r, m_group->requests()) {
1886             m_cmd->addCommand(new RemoveResourceGroupRequestCmd(r));
1887         }
1888     }
1889 }
~RemoveResourceGroupCmd()1890 RemoveResourceGroupCmd::~RemoveResourceGroupCmd()
1891 {
1892     delete m_cmd;
1893     if (m_mine)
1894         delete m_group;
1895 }
execute()1896 void RemoveResourceGroupCmd::execute()
1897 {
1898     // remove all requests to this group
1899     if (m_cmd) {
1900         m_cmd->execute();
1901     }
1902     if (m_project)
1903         m_project->takeResourceGroup(m_group);
1904     m_mine = true;
1905 
1906 
1907 }
unexecute()1908 void RemoveResourceGroupCmd::unexecute()
1909 {
1910     if (m_project)
1911         m_project->addResourceGroup(m_group, m_index);
1912 
1913     m_mine = false;
1914     // add all requests
1915     if (m_cmd) {
1916         m_cmd->unexecute();
1917     }
1918 
1919 }
1920 
AddResourceGroupCmd(Project * project,ResourceGroup * group,const KUndo2MagicString & name)1921 AddResourceGroupCmd::AddResourceGroupCmd(Project *project, ResourceGroup *group, const KUndo2MagicString& name)
1922         : RemoveResourceGroupCmd(project, group, name)
1923 {
1924     m_mine = true;
1925 }
execute()1926 void AddResourceGroupCmd::execute()
1927 {
1928     RemoveResourceGroupCmd::unexecute();
1929 }
unexecute()1930 void AddResourceGroupCmd::unexecute()
1931 {
1932     RemoveResourceGroupCmd::execute();
1933 }
1934 
ModifyResourceGroupNameCmd(ResourceGroup * group,const QString & value,const KUndo2MagicString & name)1935 ModifyResourceGroupNameCmd::ModifyResourceGroupNameCmd(ResourceGroup *group, const QString& value, const KUndo2MagicString& name)
1936         : NamedCommand(name),
1937         m_group(group),
1938         m_newvalue(value)
1939 {
1940     m_oldvalue = group->name();
1941 }
execute()1942 void ModifyResourceGroupNameCmd::execute()
1943 {
1944     m_group->setName(m_newvalue);
1945 
1946 
1947 }
unexecute()1948 void ModifyResourceGroupNameCmd::unexecute()
1949 {
1950     m_group->setName(m_oldvalue);
1951 
1952 
1953 }
1954 
ModifyResourceGroupTypeCmd(ResourceGroup * group,int value,const KUndo2MagicString & name)1955 ModifyResourceGroupTypeCmd::ModifyResourceGroupTypeCmd(ResourceGroup *group, int value, const KUndo2MagicString& name)
1956     : NamedCommand(name),
1957         m_group(group),
1958         m_newvalue(value)
1959 {
1960     m_oldvalue = group->type();
1961 }
execute()1962 void ModifyResourceGroupTypeCmd::execute()
1963 {
1964     m_group->setType(static_cast<ResourceGroup::Type>(m_newvalue));
1965 
1966 
1967 }
unexecute()1968 void ModifyResourceGroupTypeCmd::unexecute()
1969 {
1970     m_group->setType(static_cast<ResourceGroup::Type>(m_oldvalue));
1971 
1972 
1973 }
1974 
ModifyCompletionEntrymodeCmd(Completion & completion,Completion::Entrymode value,const KUndo2MagicString & name)1975 ModifyCompletionEntrymodeCmd::ModifyCompletionEntrymodeCmd(Completion &completion, Completion::Entrymode value, const KUndo2MagicString& name)
1976         : NamedCommand(name),
1977         m_completion(completion),
1978         oldvalue(m_completion.entrymode()),
1979         newvalue(value)
1980 {
1981 }
execute()1982 void ModifyCompletionEntrymodeCmd::execute()
1983 {
1984     m_completion.setEntrymode(newvalue);
1985 }
unexecute()1986 void ModifyCompletionEntrymodeCmd::unexecute()
1987 {
1988     m_completion.setEntrymode(oldvalue);
1989 }
1990 
ModifyCompletionPercentFinishedCmd(Completion & completion,const QDate & date,int value,const KUndo2MagicString & name)1991 ModifyCompletionPercentFinishedCmd::ModifyCompletionPercentFinishedCmd(Completion &completion, const QDate &date, int value, const KUndo2MagicString& name)
1992     : NamedCommand(name),
1993     m_completion(completion),
1994     m_date(date),
1995     m_newvalue(value),
1996     m_oldvalue(completion.percentFinished(date))
1997 {
1998     if (! completion.entries().contains(date)) {
1999         Completion::Entry *e = new Completion::Entry();
2000         Completion::Entry *latest = completion.entry(completion.entryDate());
2001         if (latest) {
2002             *e = *latest;
2003         }
2004         cmd.addCommand(new AddCompletionEntryCmd(completion, date, e));
2005     }
2006 
2007 }
execute()2008 void ModifyCompletionPercentFinishedCmd::execute()
2009 {
2010     cmd.execute();
2011     m_completion.setPercentFinished(m_date, m_newvalue);
2012 }
unexecute()2013 void ModifyCompletionPercentFinishedCmd::unexecute()
2014 {
2015     m_completion.setPercentFinished(m_date, m_oldvalue);
2016     cmd.unexecute();
2017 }
2018 
ModifyCompletionRemainingEffortCmd(Completion & completion,const QDate & date,const Duration & value,const KUndo2MagicString & name)2019 ModifyCompletionRemainingEffortCmd::ModifyCompletionRemainingEffortCmd(Completion &completion, const QDate &date, const Duration &value, const KUndo2MagicString &name)
2020     : NamedCommand(name),
2021     m_completion(completion),
2022     m_date(date),
2023     m_newvalue(value),
2024     m_oldvalue(completion.remainingEffort(date))
2025 {
2026     if (! completion.entries().contains(date)) {
2027         Completion::Entry *e = new Completion::Entry();
2028         Completion::Entry *latest = completion.entry(completion.entryDate());
2029         if (latest) {
2030             *e = *latest;
2031         }
2032         cmd.addCommand(new AddCompletionEntryCmd(completion, date, e));
2033     }
2034 
2035 }
execute()2036 void ModifyCompletionRemainingEffortCmd::execute()
2037 {
2038     cmd.execute();
2039     m_completion.setRemainingEffort(m_date, m_newvalue);
2040 }
unexecute()2041 void ModifyCompletionRemainingEffortCmd::unexecute()
2042 {
2043     m_completion.setRemainingEffort(m_date, m_oldvalue);
2044     cmd.unexecute();
2045 }
2046 
ModifyCompletionActualEffortCmd(Completion & completion,const QDate & date,const Duration & value,const KUndo2MagicString & name)2047 ModifyCompletionActualEffortCmd::ModifyCompletionActualEffortCmd(Completion &completion, const QDate &date, const Duration &value, const KUndo2MagicString &name)
2048     : NamedCommand(name),
2049     m_completion(completion),
2050     m_date(date),
2051     m_newvalue(value),
2052     m_oldvalue(completion.actualEffort(date))
2053 {
2054     if (! completion.entries().contains(date)) {
2055         Completion::Entry *e = new Completion::Entry();
2056         Completion::Entry *latest = completion.entry(completion.entryDate());
2057         if (latest) {
2058             *e = *latest;
2059         }
2060         cmd.addCommand(new AddCompletionEntryCmd(completion, date, e));
2061     }
2062 
2063 }
execute()2064 void ModifyCompletionActualEffortCmd::execute()
2065 {
2066     cmd.execute();
2067     m_completion.setActualEffort(m_date, m_newvalue);
2068 }
unexecute()2069 void ModifyCompletionActualEffortCmd::unexecute()
2070 {
2071     m_completion.setActualEffort(m_date, m_oldvalue);
2072     cmd.unexecute();
2073 }
2074 
ModifyCompletionStartedCmd(Completion & completion,bool value,const KUndo2MagicString & name)2075 ModifyCompletionStartedCmd::ModifyCompletionStartedCmd(Completion &completion, bool value, const KUndo2MagicString& name)
2076         : NamedCommand(name),
2077         m_completion(completion),
2078         oldvalue(m_completion.isStarted()),
2079         newvalue(value)
2080 {
2081 }
execute()2082 void ModifyCompletionStartedCmd::execute()
2083 {
2084     m_completion.setStarted(newvalue);
2085 
2086 
2087 }
unexecute()2088 void ModifyCompletionStartedCmd::unexecute()
2089 {
2090     m_completion.setStarted(oldvalue);
2091 
2092 
2093 }
2094 
ModifyCompletionFinishedCmd(Completion & completion,bool value,const KUndo2MagicString & name)2095 ModifyCompletionFinishedCmd::ModifyCompletionFinishedCmd(Completion &completion, bool value, const KUndo2MagicString& name)
2096         : NamedCommand(name),
2097         m_completion(completion),
2098         oldvalue(m_completion.isFinished()),
2099         newvalue(value)
2100 {
2101 }
execute()2102 void ModifyCompletionFinishedCmd::execute()
2103 {
2104     m_completion.setFinished(newvalue);
2105 
2106 
2107 }
unexecute()2108 void ModifyCompletionFinishedCmd::unexecute()
2109 {
2110     m_completion.setFinished(oldvalue);
2111 
2112 
2113 }
2114 
ModifyCompletionStartTimeCmd(Completion & completion,const QDateTime & value,const KUndo2MagicString & name)2115 ModifyCompletionStartTimeCmd::ModifyCompletionStartTimeCmd(Completion &completion, const QDateTime &value, const KUndo2MagicString& name)
2116         : NamedCommand(name),
2117         m_completion(completion),
2118         oldvalue(m_completion.startTime()),
2119         newvalue(value)
2120 {
2121     m_timeZone = static_cast<Project*>(completion.node()->projectNode())->timeZone();
2122 }
execute()2123 void ModifyCompletionStartTimeCmd::execute()
2124 {
2125     m_completion.setStartTime(DateTime(newvalue, m_timeZone));
2126 
2127 
2128 }
unexecute()2129 void ModifyCompletionStartTimeCmd::unexecute()
2130 {
2131     m_completion.setStartTime(oldvalue);
2132 
2133 
2134 }
2135 
ModifyCompletionFinishTimeCmd(Completion & completion,const QDateTime & value,const KUndo2MagicString & name)2136 ModifyCompletionFinishTimeCmd::ModifyCompletionFinishTimeCmd(Completion &completion, const QDateTime &value, const KUndo2MagicString& name)
2137         : NamedCommand(name),
2138         m_completion(completion),
2139         oldvalue(m_completion.finishTime()),
2140         newvalue(value)
2141 {
2142     m_timeZone = static_cast<Project*>(completion.node()->projectNode())->timeZone();
2143 }
execute()2144 void ModifyCompletionFinishTimeCmd::execute()
2145 {
2146     m_completion.setFinishTime(DateTime(newvalue, m_timeZone));
2147 
2148 
2149 }
unexecute()2150 void ModifyCompletionFinishTimeCmd::unexecute()
2151 {
2152     m_completion.setFinishTime(oldvalue);
2153 
2154 
2155 }
2156 
AddCompletionEntryCmd(Completion & completion,const QDate & date,Completion::Entry * value,const KUndo2MagicString & name)2157 AddCompletionEntryCmd::AddCompletionEntryCmd(Completion &completion, const QDate &date, Completion::Entry *value, const KUndo2MagicString& name)
2158         : NamedCommand(name),
2159         m_completion(completion),
2160         m_date(date),
2161         newvalue(value),
2162         m_newmine(true)
2163 {
2164 }
~AddCompletionEntryCmd()2165 AddCompletionEntryCmd::~AddCompletionEntryCmd()
2166 {
2167     if (m_newmine)
2168         delete newvalue;
2169 }
execute()2170 void AddCompletionEntryCmd::execute()
2171 {
2172     Q_ASSERT(! m_completion.entries().contains(m_date));
2173     m_completion.addEntry(m_date, newvalue);
2174     m_newmine = false;
2175 
2176 }
unexecute()2177 void AddCompletionEntryCmd::unexecute()
2178 {
2179     m_completion.takeEntry(m_date);
2180     m_newmine = true;
2181 
2182 }
2183 
RemoveCompletionEntryCmd(Completion & completion,const QDate & date,const KUndo2MagicString & name)2184 RemoveCompletionEntryCmd::RemoveCompletionEntryCmd(Completion &completion, const QDate &date, const KUndo2MagicString& name)
2185         : NamedCommand(name),
2186         m_completion(completion),
2187         m_date(date),
2188         m_mine(false)
2189 {
2190     value = m_completion.entry(date);
2191 }
~RemoveCompletionEntryCmd()2192 RemoveCompletionEntryCmd::~RemoveCompletionEntryCmd()
2193 {
2194     debugPlan<<m_mine<<value;
2195     if (m_mine)
2196         delete value;
2197 }
execute()2198 void RemoveCompletionEntryCmd::execute()
2199 {
2200     if (! m_completion.entries().contains(m_date)) {
2201         warnPlan<<"Completion entries does not contain date:"<<m_date;
2202     }
2203     if (value) {
2204         m_completion.takeEntry(m_date);
2205         m_mine = true;
2206     }
2207 
2208 }
unexecute()2209 void RemoveCompletionEntryCmd::unexecute()
2210 {
2211     if (value) {
2212         m_completion.addEntry(m_date, value);
2213     }
2214     m_mine = false;
2215 
2216 }
2217 
2218 
ModifyCompletionEntryCmd(Completion & completion,const QDate & date,Completion::Entry * value,const KUndo2MagicString & name)2219 ModifyCompletionEntryCmd::ModifyCompletionEntryCmd(Completion &completion, const QDate &date, Completion::Entry *value, const KUndo2MagicString& name)
2220         : NamedCommand(name)
2221 {
2222     cmd = new MacroCommand(KUndo2MagicString());
2223     cmd->addCommand(new RemoveCompletionEntryCmd(completion, date));
2224     cmd->addCommand(new AddCompletionEntryCmd(completion, date, value));
2225 }
~ModifyCompletionEntryCmd()2226 ModifyCompletionEntryCmd::~ModifyCompletionEntryCmd()
2227 {
2228     delete cmd;
2229 }
execute()2230 void ModifyCompletionEntryCmd::execute()
2231 {
2232     cmd->execute();
2233 }
unexecute()2234 void ModifyCompletionEntryCmd::unexecute()
2235 {
2236     cmd->unexecute();
2237 }
2238 
AddCompletionUsedEffortCmd(Completion & completion,const Resource * resource,Completion::UsedEffort * value,const KUndo2MagicString & name)2239 AddCompletionUsedEffortCmd::AddCompletionUsedEffortCmd(Completion &completion, const Resource *resource, Completion::UsedEffort *value, const KUndo2MagicString& name)
2240         : NamedCommand(name),
2241         m_completion(completion),
2242         m_resource(resource),
2243         newvalue(value),
2244         m_newmine(true),
2245         m_oldmine(false)
2246 {
2247     oldvalue = m_completion.usedEffort(resource);
2248 }
~AddCompletionUsedEffortCmd()2249 AddCompletionUsedEffortCmd::~AddCompletionUsedEffortCmd()
2250 {
2251     if (m_oldmine)
2252         delete oldvalue;
2253     if (m_newmine)
2254         delete newvalue;
2255 }
execute()2256 void AddCompletionUsedEffortCmd::execute()
2257 {
2258     if (oldvalue) {
2259         m_completion.takeUsedEffort(m_resource);
2260         m_oldmine = true;
2261     }
2262     m_completion.addUsedEffort(m_resource, newvalue);
2263     m_newmine = false;
2264 
2265 }
unexecute()2266 void AddCompletionUsedEffortCmd::unexecute()
2267 {
2268     m_completion.takeUsedEffort(m_resource);
2269     if (oldvalue) {
2270         m_completion.addUsedEffort(m_resource, oldvalue);
2271     }
2272     m_newmine = true;
2273     m_oldmine = false;
2274 
2275 }
2276 
AddCompletionActualEffortCmd(Task * task,Resource * resource,const QDate & date,const Completion::UsedEffort::ActualEffort & value,const KUndo2MagicString & name)2277 AddCompletionActualEffortCmd::AddCompletionActualEffortCmd(Task *task, Resource *resource, const QDate &date, const Completion::UsedEffort::ActualEffort &value, const KUndo2MagicString& name)
2278     : NamedCommand(name)
2279     , m_task(task)
2280     , m_resource(resource)
2281     , m_date(date)
2282     , newvalue(value)
2283 {
2284     oldvalue = task->completion().getActualEffort(resource, date);
2285 }
~AddCompletionActualEffortCmd()2286 AddCompletionActualEffortCmd::~AddCompletionActualEffortCmd()
2287 {
2288 }
execute()2289 void AddCompletionActualEffortCmd::execute()
2290 {
2291     m_task->completion().setActualEffort(m_resource, m_date, newvalue);
2292 }
unexecute()2293 void AddCompletionActualEffortCmd::unexecute()
2294 {
2295     m_task->completion().setActualEffort(m_resource, m_date, oldvalue);
2296 }
2297 
AddAccountCmd(Project & project,Account * account,const QString & parent,int index,const KUndo2MagicString & name)2298 AddAccountCmd::AddAccountCmd(Project &project, Account *account, const QString& parent, int index, const KUndo2MagicString& name)
2299         : NamedCommand(name),
2300         m_project(project),
2301         m_account(account),
2302         m_parent(0),
2303         m_index(index),
2304         m_parentName(parent)
2305 {
2306     m_mine = true;
2307 }
2308 
AddAccountCmd(Project & project,Account * account,Account * parent,int index,const KUndo2MagicString & name)2309 AddAccountCmd::AddAccountCmd(Project &project, Account *account, Account *parent, int index, const KUndo2MagicString& name)
2310         : NamedCommand(name),
2311         m_project(project),
2312         m_account(account),
2313         m_parent(parent),
2314         m_index(index)
2315 {
2316     m_mine = true;
2317 }
2318 
~AddAccountCmd()2319 AddAccountCmd::~AddAccountCmd()
2320 {
2321     if (m_mine)
2322         delete m_account;
2323 }
2324 
execute()2325 void AddAccountCmd::execute()
2326 {
2327     if (m_parent == 0 && !m_parentName.isEmpty()) {
2328         m_parent = m_project.accounts().findAccount(m_parentName);
2329     }
2330     m_project.accounts().insert(m_account, m_parent, m_index);
2331 
2332 
2333     m_mine = false;
2334 }
unexecute()2335 void AddAccountCmd::unexecute()
2336 {
2337     m_project.accounts().take(m_account);
2338 
2339 
2340     m_mine = true;
2341 }
2342 
RemoveAccountCmd(Project & project,Account * account,const KUndo2MagicString & name)2343 RemoveAccountCmd::RemoveAccountCmd(Project &project, Account *account, const KUndo2MagicString& name)
2344     : NamedCommand(name),
2345     m_project(project),
2346     m_account(account),
2347     m_parent(account->parent())
2348 {
2349     if (m_parent) {
2350         m_index = m_parent->accountList().indexOf(account);
2351     } else {
2352         m_index = project.accounts().accountList().indexOf(account);
2353     }
2354     m_mine = false;
2355     m_isDefault = account == project.accounts().defaultAccount();
2356 
2357     for (Account::CostPlace *cp : m_account->costPlaces()) {
2358         if (cp->node()) {
2359             if (cp->running()) {
2360                 m_cmd.addCommand(new NodeModifyRunningAccountCmd(*cp->node(), cp->node()->runningAccount(), 0));
2361             }
2362             if (cp->startup()) {
2363                 m_cmd.addCommand(new NodeModifyStartupAccountCmd(*cp->node(), cp->node()->startupAccount(), 0));
2364             }
2365             if (cp->shutdown()) {
2366                 m_cmd.addCommand(new NodeModifyShutdownAccountCmd(*cp->node(), cp->node()->shutdownAccount(), 0));
2367             }
2368         } else if (cp->resource()) {
2369             m_cmd.addCommand(new ResourceModifyAccountCmd(*cp->resource(), cp->resource()->account(), 0));
2370         }
2371     }
2372     for (int i = account->accountList().count()-1; i >= 0; --i) {
2373         m_cmd.addCommand(new RemoveAccountCmd(project, account->accountList().at(i)));
2374     }
2375 }
2376 
~RemoveAccountCmd()2377 RemoveAccountCmd::~RemoveAccountCmd()
2378 {
2379     if (m_mine)
2380         delete m_account;
2381 }
2382 
execute()2383 void RemoveAccountCmd::execute()
2384 {
2385     if (m_isDefault) {
2386         m_project.accounts().setDefaultAccount(0);
2387     }
2388     m_cmd.execute(); // remove costplaces and children
2389 
2390     m_project.accounts().take(m_account);
2391 
2392     m_mine = true;
2393 }
unexecute()2394 void RemoveAccountCmd::unexecute()
2395 {
2396     m_project.accounts().insert(m_account, m_parent, m_index);
2397 
2398     m_cmd.unexecute(); // add costplaces && children
2399 
2400     if (m_isDefault) {
2401         m_project.accounts().setDefaultAccount(m_account);
2402     }
2403 
2404     m_mine = false;
2405 }
2406 
RenameAccountCmd(Account * account,const QString & value,const KUndo2MagicString & name)2407 RenameAccountCmd::RenameAccountCmd(Account *account, const QString& value, const KUndo2MagicString& name)
2408         : NamedCommand(name),
2409         m_account(account)
2410 {
2411     m_oldvalue = account->name();
2412     m_newvalue = value;
2413 }
2414 
execute()2415 void RenameAccountCmd::execute()
2416 {
2417     m_account->setName(m_newvalue);
2418 
2419 }
unexecute()2420 void RenameAccountCmd::unexecute()
2421 {
2422     m_account->setName(m_oldvalue);
2423 
2424 }
2425 
ModifyAccountDescriptionCmd(Account * account,const QString & value,const KUndo2MagicString & name)2426 ModifyAccountDescriptionCmd::ModifyAccountDescriptionCmd(Account *account, const QString& value, const KUndo2MagicString& name)
2427         : NamedCommand(name),
2428         m_account(account)
2429 {
2430     m_oldvalue = account->description();
2431     m_newvalue = value;
2432 }
2433 
execute()2434 void ModifyAccountDescriptionCmd::execute()
2435 {
2436     m_account->setDescription(m_newvalue);
2437 
2438 }
unexecute()2439 void ModifyAccountDescriptionCmd::unexecute()
2440 {
2441     m_account->setDescription(m_oldvalue);
2442 
2443 }
2444 
2445 
NodeModifyStartupCostCmd(Node & node,double value,const KUndo2MagicString & name)2446 NodeModifyStartupCostCmd::NodeModifyStartupCostCmd(Node &node, double value, const KUndo2MagicString& name)
2447         : NamedCommand(name),
2448         m_node(node)
2449 {
2450     m_oldvalue = node.startupCost();
2451     m_newvalue = value;
2452 }
2453 
execute()2454 void NodeModifyStartupCostCmd::execute()
2455 {
2456     m_node.setStartupCost(m_newvalue);
2457 
2458 }
unexecute()2459 void NodeModifyStartupCostCmd::unexecute()
2460 {
2461     m_node.setStartupCost(m_oldvalue);
2462 
2463 }
2464 
NodeModifyShutdownCostCmd(Node & node,double value,const KUndo2MagicString & name)2465 NodeModifyShutdownCostCmd::NodeModifyShutdownCostCmd(Node &node, double value, const KUndo2MagicString& name)
2466         : NamedCommand(name),
2467         m_node(node)
2468 {
2469     m_oldvalue = node.shutdownCost();
2470     m_newvalue = value;
2471 }
2472 
execute()2473 void NodeModifyShutdownCostCmd::execute()
2474 {
2475     m_node.setShutdownCost(m_newvalue);
2476 
2477 }
unexecute()2478 void NodeModifyShutdownCostCmd::unexecute()
2479 {
2480     m_node.setShutdownCost(m_oldvalue);
2481 
2482 }
2483 
NodeModifyRunningAccountCmd(Node & node,Account * oldvalue,Account * newvalue,const KUndo2MagicString & name)2484 NodeModifyRunningAccountCmd::NodeModifyRunningAccountCmd(Node &node, Account *oldvalue, Account *newvalue, const KUndo2MagicString& name)
2485         : NamedCommand(name),
2486         m_node(node)
2487 {
2488     m_oldvalue = oldvalue;
2489     m_newvalue = newvalue;
2490     //debugPlan;
2491 }
execute()2492 void NodeModifyRunningAccountCmd::execute()
2493 {
2494     //debugPlan;
2495     if (m_oldvalue) {
2496         m_oldvalue->removeRunning(m_node);
2497     }
2498     if (m_newvalue) {
2499         m_newvalue->addRunning(m_node);
2500     }
2501 
2502 }
unexecute()2503 void NodeModifyRunningAccountCmd::unexecute()
2504 {
2505     //debugPlan;
2506     if (m_newvalue) {
2507         m_newvalue->removeRunning(m_node);
2508     }
2509     if (m_oldvalue) {
2510         m_oldvalue->addRunning(m_node);
2511     }
2512 
2513 }
2514 
NodeModifyStartupAccountCmd(Node & node,Account * oldvalue,Account * newvalue,const KUndo2MagicString & name)2515 NodeModifyStartupAccountCmd::NodeModifyStartupAccountCmd(Node &node, Account *oldvalue, Account *newvalue, const KUndo2MagicString& name)
2516         : NamedCommand(name),
2517         m_node(node)
2518 {
2519     m_oldvalue = oldvalue;
2520     m_newvalue = newvalue;
2521     //debugPlan;
2522 }
2523 
execute()2524 void NodeModifyStartupAccountCmd::execute()
2525 {
2526     //debugPlan;
2527     if (m_oldvalue) {
2528         m_oldvalue->removeStartup(m_node);
2529     }
2530     if (m_newvalue) {
2531         m_newvalue->addStartup(m_node);
2532     }
2533 
2534 }
unexecute()2535 void NodeModifyStartupAccountCmd::unexecute()
2536 {
2537     //debugPlan;
2538     if (m_newvalue) {
2539         m_newvalue->removeStartup(m_node);
2540     }
2541     if (m_oldvalue) {
2542         m_oldvalue->addStartup(m_node);
2543     }
2544 
2545 }
2546 
NodeModifyShutdownAccountCmd(Node & node,Account * oldvalue,Account * newvalue,const KUndo2MagicString & name)2547 NodeModifyShutdownAccountCmd::NodeModifyShutdownAccountCmd(Node &node, Account *oldvalue, Account *newvalue, const KUndo2MagicString& name)
2548         : NamedCommand(name),
2549         m_node(node)
2550 {
2551     m_oldvalue = oldvalue;
2552     m_newvalue = newvalue;
2553     //debugPlan;
2554 }
2555 
execute()2556 void NodeModifyShutdownAccountCmd::execute()
2557 {
2558     //debugPlan;
2559     if (m_oldvalue) {
2560         m_oldvalue->removeShutdown(m_node);
2561     }
2562     if (m_newvalue) {
2563         m_newvalue->addShutdown(m_node);
2564     }
2565 
2566 }
unexecute()2567 void NodeModifyShutdownAccountCmd::unexecute()
2568 {
2569     //debugPlan;
2570     if (m_newvalue) {
2571         m_newvalue->removeShutdown(m_node);
2572     }
2573     if (m_oldvalue) {
2574         m_oldvalue->addShutdown(m_node);
2575     }
2576 
2577 }
2578 
ModifyDefaultAccountCmd(Accounts & acc,Account * oldvalue,Account * newvalue,const KUndo2MagicString & name)2579 ModifyDefaultAccountCmd::ModifyDefaultAccountCmd(Accounts &acc, Account *oldvalue, Account *newvalue, const KUndo2MagicString& name)
2580         : NamedCommand(name),
2581         m_accounts(acc)
2582 {
2583     m_oldvalue = oldvalue;
2584     m_newvalue = newvalue;
2585     //debugPlan;
2586 }
2587 
execute()2588 void ModifyDefaultAccountCmd::execute()
2589 {
2590     //debugPlan;
2591     m_accounts.setDefaultAccount(m_newvalue);
2592 
2593 }
unexecute()2594 void ModifyDefaultAccountCmd::unexecute()
2595 {
2596     //debugPlan;
2597     m_accounts.setDefaultAccount(m_oldvalue);
2598 
2599 }
2600 
ResourceModifyAccountCmd(Resource & resource,Account * oldvalue,Account * newvalue,const KUndo2MagicString & name)2601 ResourceModifyAccountCmd::ResourceModifyAccountCmd(Resource &resource,  Account *oldvalue, Account *newvalue, const KUndo2MagicString& name)
2602     : NamedCommand(name),
2603     m_resource(resource)
2604 {
2605     m_oldvalue = oldvalue;
2606     m_newvalue = newvalue;
2607 }
execute()2608 void ResourceModifyAccountCmd::execute()
2609 {
2610     //debugPlan;
2611     if (m_oldvalue) {
2612         m_oldvalue->removeRunning(m_resource);
2613     }
2614     if (m_newvalue) {
2615         m_newvalue->addRunning(m_resource);
2616     }
2617 }
unexecute()2618 void ResourceModifyAccountCmd::unexecute()
2619 {
2620     //debugPlan;
2621     if (m_newvalue) {
2622         m_newvalue->removeRunning(m_resource);
2623     }
2624     if (m_oldvalue) {
2625         m_oldvalue->addRunning(m_resource);
2626     }
2627 }
2628 
ProjectModifyConstraintCmd(Project & node,Node::ConstraintType c,const KUndo2MagicString & name)2629 ProjectModifyConstraintCmd::ProjectModifyConstraintCmd(Project &node, Node::ConstraintType c, const KUndo2MagicString& name)
2630         : NamedCommand(name),
2631         m_node(node),
2632         newConstraint(c),
2633         oldConstraint(static_cast<Node::ConstraintType>(node.constraint()))
2634 {
2635 }
execute()2636 void ProjectModifyConstraintCmd::execute()
2637 {
2638     m_node.setConstraint(newConstraint);
2639 }
unexecute()2640 void ProjectModifyConstraintCmd::unexecute()
2641 {
2642     m_node.setConstraint(oldConstraint);
2643 }
2644 
ProjectModifyStartTimeCmd(Project & node,const QDateTime & dt,const KUndo2MagicString & name)2645 ProjectModifyStartTimeCmd::ProjectModifyStartTimeCmd(Project &node, const QDateTime& dt, const KUndo2MagicString& name)
2646         : NamedCommand(name),
2647         m_node(node),
2648         newTime(dt),
2649         oldTime(node.startTime())
2650 {
2651     m_timeZone = node.timeZone();
2652 }
2653 
execute()2654 void ProjectModifyStartTimeCmd::execute()
2655 {
2656     m_node.setConstraintStartTime(DateTime(newTime, m_timeZone));
2657 }
unexecute()2658 void ProjectModifyStartTimeCmd::unexecute()
2659 {
2660     m_node.setConstraintStartTime(oldTime);
2661 }
2662 
ProjectModifyEndTimeCmd(Project & node,const QDateTime & dt,const KUndo2MagicString & name)2663 ProjectModifyEndTimeCmd::ProjectModifyEndTimeCmd(Project &node, const QDateTime& dt, const KUndo2MagicString& name)
2664         : NamedCommand(name),
2665         m_node(node),
2666         newTime(dt),
2667         oldTime(node.endTime())
2668 {
2669     m_timeZone = node.timeZone();
2670 }
execute()2671 void ProjectModifyEndTimeCmd::execute()
2672 {
2673     m_node.setEndTime(DateTime(newTime, m_timeZone));
2674     m_node.setConstraintEndTime(DateTime(newTime, m_timeZone));
2675 }
unexecute()2676 void ProjectModifyEndTimeCmd::unexecute()
2677 {
2678     m_node.setConstraintEndTime(oldTime);
2679 }
2680 
ProjectModifyWorkPackageInfoCmd(Project & project,const Project::WorkPackageInfo & wpi,const KUndo2MagicString & name)2681 ProjectModifyWorkPackageInfoCmd::ProjectModifyWorkPackageInfoCmd(Project &project, const Project::WorkPackageInfo &wpi, const KUndo2MagicString& name)
2682     : NamedCommand(name)
2683     , m_node(project)
2684     , m_newWpi(wpi)
2685     , m_oldWpi(project.workPackageInfo())
2686 {
2687 }
execute()2688 void ProjectModifyWorkPackageInfoCmd::execute()
2689 {
2690     m_node.setWorkPackageInfo(m_newWpi);
2691 }
unexecute()2692 void ProjectModifyWorkPackageInfoCmd::unexecute()
2693 {
2694     m_node.setWorkPackageInfo(m_oldWpi);
2695 }
2696 
2697 //----------------------------
SwapScheduleManagerCmd(Project & project,ScheduleManager * from,ScheduleManager * to,const KUndo2MagicString & name)2698 SwapScheduleManagerCmd::SwapScheduleManagerCmd(Project &project, ScheduleManager *from, ScheduleManager *to, const KUndo2MagicString& name)
2699     : NamedCommand(name),
2700     m_node(project),
2701     m_from(from),
2702     m_to(to)
2703 {
2704 }
2705 
~SwapScheduleManagerCmd()2706 SwapScheduleManagerCmd::~SwapScheduleManagerCmd()
2707 {
2708 }
2709 
execute()2710 void SwapScheduleManagerCmd::execute()
2711 {
2712     m_node.swapScheduleManagers(m_from, m_to);
2713 }
2714 
unexecute()2715 void SwapScheduleManagerCmd::unexecute()
2716 {
2717     m_node.swapScheduleManagers(m_to, m_from);
2718 }
2719 
AddScheduleManagerCmd(Project & node,ScheduleManager * sm,int index,const KUndo2MagicString & name)2720 AddScheduleManagerCmd::AddScheduleManagerCmd(Project &node, ScheduleManager *sm, int index, const KUndo2MagicString& name)
2721     : NamedCommand(name),
2722     m_node(node),
2723     m_parent(sm->parentManager()),
2724     m_sm(sm),
2725     m_index(index),
2726     m_exp(sm->expected()),
2727     m_mine(true)
2728 {
2729 }
2730 
AddScheduleManagerCmd(ScheduleManager * parent,ScheduleManager * sm,int index,const KUndo2MagicString & name)2731 AddScheduleManagerCmd::AddScheduleManagerCmd(ScheduleManager *parent, ScheduleManager *sm, int index, const KUndo2MagicString& name)
2732     : NamedCommand(name),
2733     m_node(parent->project()),
2734     m_parent(parent),
2735     m_sm(sm),
2736     m_index(index),
2737     m_exp(sm->expected()),
2738     m_mine(true)
2739 {
2740     if (parent->schedulingMode() == ScheduleManager::AutoMode) {
2741         m_cmd.addCommand(new ModifyScheduleManagerSchedulingModeCmd(*parent, ScheduleManager::ManualMode));
2742     }
2743 }
2744 
~AddScheduleManagerCmd()2745 AddScheduleManagerCmd::~AddScheduleManagerCmd()
2746 {
2747     if (m_mine) {
2748         m_sm->setParentManager(0);
2749         delete m_sm;
2750     }
2751 }
2752 
execute()2753 void AddScheduleManagerCmd::execute()
2754 {
2755     m_cmd.redo();
2756     m_node.addScheduleManager(m_sm, m_parent, m_index);
2757     m_sm->setExpected(m_exp);
2758     m_mine = false;
2759 }
2760 
unexecute()2761 void AddScheduleManagerCmd::unexecute()
2762 {
2763     m_node.takeScheduleManager(m_sm);
2764     m_sm->setExpected(0);
2765     m_mine = true;
2766     m_cmd.undo();
2767 }
2768 
DeleteScheduleManagerCmd(Project & node,ScheduleManager * sm,const KUndo2MagicString & name)2769 DeleteScheduleManagerCmd::DeleteScheduleManagerCmd(Project &node, ScheduleManager *sm, const KUndo2MagicString& name)
2770     : AddScheduleManagerCmd(node, sm, -1, name)
2771 {
2772     m_mine = false;
2773     m_index = m_parent ? m_parent->indexOf(sm) : node.indexOf(sm);
2774     foreach (ScheduleManager *s, sm->children()) {
2775         cmd.addCommand(new DeleteScheduleManagerCmd(node, s));
2776     }
2777 }
2778 
execute()2779 void DeleteScheduleManagerCmd::execute()
2780 {
2781     cmd.execute();
2782     AddScheduleManagerCmd::unexecute();
2783 }
2784 
unexecute()2785 void DeleteScheduleManagerCmd::unexecute()
2786 {
2787     AddScheduleManagerCmd::execute();
2788     cmd.unexecute();
2789 }
2790 
MoveScheduleManagerCmd(ScheduleManager * sm,ScheduleManager * newparent,int newindex,const KUndo2MagicString & name)2791 MoveScheduleManagerCmd::MoveScheduleManagerCmd(ScheduleManager *sm, ScheduleManager *newparent, int newindex, const KUndo2MagicString& name)
2792     : NamedCommand(name),
2793     m_sm(sm),
2794     m_oldparent(sm->parentManager()),
2795     m_newparent(newparent),
2796     m_newindex(newindex)
2797 {
2798     m_oldindex = sm->parentManager() ? sm->parentManager()->indexOf(sm) : sm->project().indexOf(sm);
2799 }
2800 
execute()2801 void MoveScheduleManagerCmd::execute()
2802 {
2803     m_sm->project().moveScheduleManager(m_sm, m_newparent, m_newindex);
2804 }
2805 
unexecute()2806 void MoveScheduleManagerCmd::unexecute()
2807 {
2808     m_sm->project().moveScheduleManager(m_sm, m_oldparent, m_oldindex);
2809 }
2810 
ModifyScheduleManagerNameCmd(ScheduleManager & sm,const QString & value,const KUndo2MagicString & name)2811 ModifyScheduleManagerNameCmd::ModifyScheduleManagerNameCmd(ScheduleManager &sm, const QString& value, const KUndo2MagicString& name)
2812     : NamedCommand(name),
2813     m_sm(sm),
2814     oldvalue(sm.name()),
2815     newvalue(value)
2816 {
2817 }
2818 
execute()2819 void ModifyScheduleManagerNameCmd::execute()
2820 {
2821     m_sm.setName(newvalue);
2822 }
2823 
unexecute()2824 void ModifyScheduleManagerNameCmd::unexecute()
2825 {
2826     m_sm.setName(oldvalue);
2827 }
2828 
ModifyScheduleManagerSchedulingModeCmd(ScheduleManager & sm,int value,const KUndo2MagicString & name)2829 ModifyScheduleManagerSchedulingModeCmd::ModifyScheduleManagerSchedulingModeCmd(ScheduleManager &sm, int value, const KUndo2MagicString& name)
2830     : NamedCommand(name),
2831     m_sm(sm),
2832     oldvalue(sm.schedulingMode()),
2833     newvalue(value)
2834 {
2835     if (value == ScheduleManager::AutoMode) {
2836         // Allow only one
2837         for (ScheduleManager *m : sm.project().allScheduleManagers()) {
2838             if (m->schedulingMode() == ScheduleManager::AutoMode) {
2839                 m_cmd.addCommand(new ModifyScheduleManagerSchedulingModeCmd(*m, ScheduleManager::ManualMode));
2840                 break;
2841             }
2842         }
2843     }
2844 }
2845 
execute()2846 void ModifyScheduleManagerSchedulingModeCmd::execute()
2847 {
2848     m_cmd.redo();
2849     m_sm.setSchedulingMode(newvalue);
2850 }
2851 
unexecute()2852 void ModifyScheduleManagerSchedulingModeCmd::unexecute()
2853 {
2854     m_sm.setSchedulingMode(oldvalue);
2855     m_cmd.undo();
2856 }
2857 
ModifyScheduleManagerAllowOverbookingCmd(ScheduleManager & sm,bool value,const KUndo2MagicString & name)2858 ModifyScheduleManagerAllowOverbookingCmd::ModifyScheduleManagerAllowOverbookingCmd(ScheduleManager &sm, bool value, const KUndo2MagicString& name)
2859     : NamedCommand(name),
2860     m_sm(sm),
2861     oldvalue(sm.allowOverbooking()),
2862     newvalue(value)
2863 {
2864 }
2865 
execute()2866 void ModifyScheduleManagerAllowOverbookingCmd::execute()
2867 {
2868     m_sm.setAllowOverbooking(newvalue);
2869 }
2870 
unexecute()2871 void ModifyScheduleManagerAllowOverbookingCmd::unexecute()
2872 {
2873     m_sm.setAllowOverbooking(oldvalue);
2874 }
2875 
ModifyScheduleManagerDistributionCmd(ScheduleManager & sm,bool value,const KUndo2MagicString & name)2876 ModifyScheduleManagerDistributionCmd::ModifyScheduleManagerDistributionCmd(ScheduleManager &sm, bool value, const KUndo2MagicString& name)
2877     : NamedCommand(name),
2878     m_sm(sm),
2879     oldvalue(sm.usePert()),
2880     newvalue(value)
2881 {
2882 }
2883 
execute()2884 void ModifyScheduleManagerDistributionCmd::execute()
2885 {
2886     m_sm.setUsePert(newvalue);
2887 }
2888 
unexecute()2889 void ModifyScheduleManagerDistributionCmd::unexecute()
2890 {
2891     m_sm.setUsePert(oldvalue);
2892 }
2893 
ModifyScheduleManagerSchedulingDirectionCmd(ScheduleManager & sm,bool value,const KUndo2MagicString & name)2894 ModifyScheduleManagerSchedulingDirectionCmd::ModifyScheduleManagerSchedulingDirectionCmd(ScheduleManager &sm, bool value, const KUndo2MagicString& name)
2895     : NamedCommand(name),
2896     m_sm(sm),
2897     oldvalue(sm.schedulingDirection()),
2898     newvalue(value)
2899 {
2900 }
2901 
execute()2902 void ModifyScheduleManagerSchedulingDirectionCmd::execute()
2903 {
2904     m_sm.setSchedulingDirection(newvalue);
2905 }
2906 
unexecute()2907 void ModifyScheduleManagerSchedulingDirectionCmd::unexecute()
2908 {
2909     m_sm.setSchedulingDirection(oldvalue);
2910 }
2911 
ModifyScheduleManagerSchedulerCmd(ScheduleManager & sm,int value,const KUndo2MagicString & name)2912 ModifyScheduleManagerSchedulerCmd::ModifyScheduleManagerSchedulerCmd(ScheduleManager &sm, int value, const KUndo2MagicString& name)
2913     : NamedCommand(name),
2914     m_sm(sm),
2915     oldvalue(sm.schedulerPluginIndex()),
2916     newvalue(value)
2917 {
2918 }
2919 
execute()2920 void ModifyScheduleManagerSchedulerCmd::execute()
2921 {
2922     m_sm.setSchedulerPlugin(newvalue);
2923 }
2924 
unexecute()2925 void ModifyScheduleManagerSchedulerCmd::unexecute()
2926 {
2927     m_sm.setSchedulerPlugin(oldvalue);
2928 }
2929 
ModifyScheduleManagerSchedulingGranularityCmd(ScheduleManager & sm,int value,const KUndo2MagicString & name)2930 ModifyScheduleManagerSchedulingGranularityCmd::ModifyScheduleManagerSchedulingGranularityCmd(ScheduleManager &sm, int value, const KUndo2MagicString& name)
2931     : NamedCommand(name),
2932     m_sm(sm),
2933     oldvalue(sm.granularity()),
2934     newvalue(value)
2935 {
2936 }
2937 
execute()2938 void ModifyScheduleManagerSchedulingGranularityCmd::execute()
2939 {
2940     m_sm.setGranularity(newvalue);
2941 }
2942 
unexecute()2943 void ModifyScheduleManagerSchedulingGranularityCmd::unexecute()
2944 {
2945     m_sm.setGranularity(oldvalue);
2946 }
2947 
CalculateScheduleCmd(Project & node,ScheduleManager * sm,const KUndo2MagicString & name)2948 CalculateScheduleCmd::CalculateScheduleCmd(Project &node, ScheduleManager *sm, const KUndo2MagicString& name)
2949     : NamedCommand(name),
2950     m_node(node),
2951     m_sm(sm),
2952     m_first(true),
2953     m_newexpected(0)
2954 {
2955     if (sm->recalculate() && sm->isScheduled()) {
2956         m_sm = new ScheduleManager(node);
2957         m_sm->setRecalculate(true);
2958         m_sm->setGranularity(sm->granularity());
2959         m_sm->setUsePert(sm->usePert());
2960         m_sm->setSchedulerPluginId(sm->schedulerPluginId());
2961         m_sm->setAllowOverbooking(sm->allowOverbooking());
2962         m_sm->setName(sm->name());
2963 
2964         preCmd.addCommand(new AddScheduleManagerCmd(sm, m_sm));
2965 
2966         postCmd.addCommand(new SwapScheduleManagerCmd(node, sm, m_sm));
2967         postCmd.addCommand(new MoveScheduleManagerCmd(m_sm, sm->parentManager(), sm->parentManager() ? sm->parentManager()->indexOf(sm)+1 : node.indexOf(sm)+1));
2968         postCmd.addCommand(new DeleteScheduleManagerCmd(node, sm));
2969     }
2970     m_oldexpected = m_sm->expected();
2971 }
2972 
~CalculateScheduleCmd()2973 CalculateScheduleCmd::~CalculateScheduleCmd()
2974 {
2975     if (m_sm->scheduling()) {
2976         m_sm->haltCalculation();
2977     }
2978 }
2979 
execute()2980 void CalculateScheduleCmd::execute()
2981 {
2982     Q_ASSERT(m_sm);
2983     preCmd.redo();
2984     if (m_first) {
2985         m_sm->calculateSchedule();
2986         if (m_sm->calculationResult() != ScheduleManager::CalculationCanceled) {
2987             m_first = false;
2988         }
2989         m_newexpected = m_sm->expected();
2990     } else {
2991         m_sm->setExpected(m_newexpected);
2992     }
2993     postCmd.redo();
2994 }
2995 
unexecute()2996 void CalculateScheduleCmd::unexecute()
2997 {
2998     if (m_sm->scheduling()) {
2999         // terminate scheduling
3000         QApplication::setOverrideCursor(Qt::WaitCursor);
3001         m_sm->haltCalculation();
3002         m_first = true;
3003         QApplication::restoreOverrideCursor();
3004 
3005     }
3006     postCmd.undo();
3007     m_sm->setExpected(m_oldexpected);
3008     preCmd.undo();
3009 }
3010 
3011 //------------------------
BaselineScheduleCmd(ScheduleManager & sm,const KUndo2MagicString & name)3012 BaselineScheduleCmd::BaselineScheduleCmd(ScheduleManager &sm, const KUndo2MagicString& name)
3013     : NamedCommand(name),
3014     m_sm(sm)
3015 {
3016 }
3017 
execute()3018 void BaselineScheduleCmd::execute()
3019 {
3020     m_sm.setBaselined(true);
3021 }
3022 
unexecute()3023 void BaselineScheduleCmd::unexecute()
3024 {
3025     m_sm.setBaselined(false);
3026 }
3027 
ResetBaselineScheduleCmd(ScheduleManager & sm,const KUndo2MagicString & name)3028 ResetBaselineScheduleCmd::ResetBaselineScheduleCmd(ScheduleManager &sm, const KUndo2MagicString& name)
3029     : NamedCommand(name),
3030     m_sm(sm)
3031 {
3032 }
3033 
execute()3034 void ResetBaselineScheduleCmd::execute()
3035 {
3036     m_sm.setBaselined(false);
3037 }
3038 
unexecute()3039 void ResetBaselineScheduleCmd::unexecute()
3040 {
3041     m_sm.setBaselined(true);
3042 }
3043 
3044 //------------------------
ModifyStandardWorktimeYearCmd(StandardWorktime * wt,double oldvalue,double newvalue,const KUndo2MagicString & name)3045 ModifyStandardWorktimeYearCmd::ModifyStandardWorktimeYearCmd(StandardWorktime *wt, double oldvalue, double newvalue, const KUndo2MagicString& name)
3046         : NamedCommand(name),
3047         swt(wt),
3048         m_oldvalue(oldvalue),
3049         m_newvalue(newvalue)
3050 {
3051 }
execute()3052 void ModifyStandardWorktimeYearCmd::execute()
3053 {
3054     swt->setYear(m_newvalue);
3055 
3056 }
unexecute()3057 void ModifyStandardWorktimeYearCmd::unexecute()
3058 {
3059     swt->setYear(m_oldvalue);
3060 
3061 }
3062 
ModifyStandardWorktimeMonthCmd(StandardWorktime * wt,double oldvalue,double newvalue,const KUndo2MagicString & name)3063 ModifyStandardWorktimeMonthCmd::ModifyStandardWorktimeMonthCmd(StandardWorktime *wt, double oldvalue, double newvalue, const KUndo2MagicString& name)
3064         : NamedCommand(name),
3065         swt(wt),
3066         m_oldvalue(oldvalue),
3067         m_newvalue(newvalue)
3068 {
3069 }
execute()3070 void ModifyStandardWorktimeMonthCmd::execute()
3071 {
3072     swt->setMonth(m_newvalue);
3073 
3074 }
unexecute()3075 void ModifyStandardWorktimeMonthCmd::unexecute()
3076 {
3077     swt->setMonth(m_oldvalue);
3078 
3079 }
3080 
ModifyStandardWorktimeWeekCmd(StandardWorktime * wt,double oldvalue,double newvalue,const KUndo2MagicString & name)3081 ModifyStandardWorktimeWeekCmd::ModifyStandardWorktimeWeekCmd(StandardWorktime *wt, double oldvalue, double newvalue, const KUndo2MagicString& name)
3082         : NamedCommand(name),
3083         swt(wt),
3084         m_oldvalue(oldvalue),
3085         m_newvalue(newvalue)
3086 {
3087 }
execute()3088 void ModifyStandardWorktimeWeekCmd::execute()
3089 {
3090     swt->setWeek(m_newvalue);
3091 
3092 }
unexecute()3093 void ModifyStandardWorktimeWeekCmd::unexecute()
3094 {
3095     swt->setWeek(m_oldvalue);
3096 
3097 }
3098 
ModifyStandardWorktimeDayCmd(StandardWorktime * wt,double oldvalue,double newvalue,const KUndo2MagicString & name)3099 ModifyStandardWorktimeDayCmd::ModifyStandardWorktimeDayCmd(StandardWorktime *wt, double oldvalue, double newvalue, const KUndo2MagicString& name)
3100         : NamedCommand(name),
3101         swt(wt),
3102         m_oldvalue(oldvalue),
3103         m_newvalue(newvalue)
3104 {
3105 }
3106 
execute()3107 void ModifyStandardWorktimeDayCmd::execute()
3108 {
3109     swt->setDay(m_newvalue);
3110 
3111 }
unexecute()3112 void ModifyStandardWorktimeDayCmd::unexecute()
3113 {
3114     swt->setDay(m_oldvalue);
3115 
3116 }
3117 
3118 //----------------
DocumentAddCmd(Documents & docs,Document * value,const KUndo2MagicString & name)3119 DocumentAddCmd::DocumentAddCmd(Documents &docs, Document *value, const KUndo2MagicString& name)
3120     : NamedCommand(name),
3121     m_docs(docs),
3122     m_mine(true)
3123 {
3124     Q_ASSERT(value);
3125     m_value = value;
3126 }
~DocumentAddCmd()3127 DocumentAddCmd::~DocumentAddCmd()
3128 {
3129     //debugPlan;
3130     if (m_mine)
3131         delete m_value;
3132 }
execute()3133 void DocumentAddCmd::execute()
3134 {
3135     m_docs.addDocument(m_value);
3136     m_mine = false;
3137 }
unexecute()3138 void DocumentAddCmd::unexecute()
3139 {
3140     m_docs.takeDocument(m_value);
3141     m_mine = true;
3142 }
3143 
3144 //----------------
DocumentRemoveCmd(Documents & docs,Document * value,const KUndo2MagicString & name)3145 DocumentRemoveCmd::DocumentRemoveCmd(Documents &docs, Document *value, const KUndo2MagicString& name)
3146     : NamedCommand(name),
3147     m_docs(docs),
3148     m_mine(false)
3149 {
3150     Q_ASSERT(value);
3151     m_value = value;
3152 }
~DocumentRemoveCmd()3153 DocumentRemoveCmd::~DocumentRemoveCmd()
3154 {
3155     //debugPlan;
3156     if (m_mine)
3157         delete m_value;
3158 }
execute()3159 void DocumentRemoveCmd::execute()
3160 {
3161     m_docs.takeDocument(m_value);
3162     m_mine = true;
3163 }
unexecute()3164 void DocumentRemoveCmd::unexecute()
3165 {
3166     m_docs.addDocument(m_value);
3167     m_mine = false;
3168 }
3169 
3170 //----------------
DocumentModifyUrlCmd(Document * doc,const QUrl & value,const KUndo2MagicString & name)3171 DocumentModifyUrlCmd::DocumentModifyUrlCmd(Document *doc, const QUrl &value, const KUndo2MagicString& name)
3172     : NamedCommand(name),
3173     m_doc(doc)
3174 {
3175     Q_ASSERT(doc);
3176     m_value = value;
3177     m_oldvalue = doc->url();
3178 }
execute()3179 void DocumentModifyUrlCmd::execute()
3180 {
3181     m_doc->setUrl(m_value);
3182 }
unexecute()3183 void DocumentModifyUrlCmd::unexecute()
3184 {
3185     m_doc->setUrl(m_oldvalue);
3186 }
3187 
3188 //----------------
DocumentModifyNameCmd(Document * doc,const QString & value,const KUndo2MagicString & name)3189 DocumentModifyNameCmd::DocumentModifyNameCmd(Document *doc, const QString &value, const KUndo2MagicString& name)
3190     : NamedCommand(name),
3191     m_doc(doc)
3192 {
3193     Q_ASSERT(doc);
3194     m_value = value;
3195     m_oldvalue = doc->name();
3196 }
execute()3197 void DocumentModifyNameCmd::execute()
3198 {
3199     m_doc->setName(m_value);
3200 }
unexecute()3201 void DocumentModifyNameCmd::unexecute()
3202 {
3203     m_doc->setName(m_oldvalue);
3204 }
3205 
3206 //----------------
DocumentModifyTypeCmd(Document * doc,Document::Type value,const KUndo2MagicString & name)3207 DocumentModifyTypeCmd::DocumentModifyTypeCmd(Document *doc, Document::Type value, const KUndo2MagicString& name)
3208     : NamedCommand(name),
3209     m_doc(doc)
3210 {
3211     Q_ASSERT(doc);
3212     m_value = value;
3213     m_oldvalue = doc->type();
3214 }
execute()3215 void DocumentModifyTypeCmd::execute()
3216 {
3217     m_doc->setType(m_value);
3218 }
unexecute()3219 void DocumentModifyTypeCmd::unexecute()
3220 {
3221     m_doc->setType(m_oldvalue);
3222 }
3223 
3224 //----------------
DocumentModifyStatusCmd(Document * doc,const QString & value,const KUndo2MagicString & name)3225 DocumentModifyStatusCmd::DocumentModifyStatusCmd(Document *doc, const QString &value, const KUndo2MagicString& name)
3226     : NamedCommand(name),
3227     m_doc(doc)
3228 {
3229     Q_ASSERT(doc);
3230     m_value = value;
3231     m_oldvalue = doc->type();
3232 }
execute()3233 void DocumentModifyStatusCmd::execute()
3234 {
3235     m_doc->setStatus(m_value);
3236 }
unexecute()3237 void DocumentModifyStatusCmd::unexecute()
3238 {
3239     m_doc->setStatus(m_oldvalue);
3240 }
3241 
3242 //----------------
DocumentModifySendAsCmd(Document * doc,const Document::SendAs value,const KUndo2MagicString & name)3243 DocumentModifySendAsCmd::DocumentModifySendAsCmd(Document *doc, const Document::SendAs value, const KUndo2MagicString& name)
3244     : NamedCommand(name),
3245     m_doc(doc)
3246 {
3247     Q_ASSERT(doc);
3248     m_value = value;
3249     m_oldvalue = doc->sendAs();
3250 }
execute()3251 void DocumentModifySendAsCmd::execute()
3252 {
3253     m_doc->setSendAs(m_value);
3254 }
unexecute()3255 void DocumentModifySendAsCmd::unexecute()
3256 {
3257     m_doc->setSendAs(m_oldvalue);
3258 }
3259 
3260 //----------------
WBSDefinitionModifyCmd(Project & project,const WBSDefinition value,const KUndo2MagicString & name)3261 WBSDefinitionModifyCmd::WBSDefinitionModifyCmd(Project &project, const WBSDefinition value, const KUndo2MagicString& name)
3262     : NamedCommand(name),
3263     m_project(project)
3264 {
3265     m_newvalue = value;
3266     m_oldvalue = m_project.wbsDefinition();
3267 }
execute()3268 void WBSDefinitionModifyCmd::execute()
3269 {
3270     m_project.setWbsDefinition(m_newvalue);
3271 }
unexecute()3272 void WBSDefinitionModifyCmd::unexecute()
3273 {
3274     m_project.setWbsDefinition(m_oldvalue);
3275 }
3276 
3277 //----------------
InsertProjectCmd(Project & project,Node * parent,Node * after,const KUndo2MagicString & name)3278 InsertProjectCmd::InsertProjectCmd(Project &project, Node *parent, Node *after, const KUndo2MagicString& name)
3279     : MacroCommand(name),
3280     m_project(static_cast<Project*>(parent->projectNode())),
3281     m_parent(parent)
3282 {
3283     Q_ASSERT(&project != m_project);
3284 
3285     if (m_project->defaultCalendar()) {
3286         project.setDefaultCalendar(0); // or else m_project default calendar may be overwitten
3287     }
3288     QString defaultAccount;
3289     if (! m_project->accounts().defaultAccount() && project.accounts().defaultAccount()) {
3290         defaultAccount = project.accounts().defaultAccount()->name();
3291     }
3292 
3293     QHash<Node*, QString> startupaccountmap;
3294     QHash<Node*, QString> shutdownaccountmap;
3295     QHash<Node*, QString> runningaccountmap;
3296     QHash<Node*, QString> nodecalendarmap;
3297 
3298     // remove unhandled info in tasks and get accounts and calendars
3299     foreach (Node *n, project.allNodes()) {
3300         if (n->type() == Node::Type_Task) {
3301             Task *t = static_cast<Task*>(n);
3302             t->workPackage().clear();
3303             while (! t->workPackageLog().isEmpty()) {
3304                 WorkPackage *wp = t->workPackageLog().at(0);
3305                 t->removeWorkPackage(wp);
3306                 delete wp;
3307             }
3308         }
3309         if (n->startupAccount()) {
3310             startupaccountmap.insert(n, n->startupAccount()->name());
3311             n->setStartupAccount(0);
3312         }
3313         if (n->shutdownAccount()) {
3314             shutdownaccountmap.insert(n, n->shutdownAccount()->name());
3315             n->setShutdownAccount(0);
3316         }
3317         if (n->runningAccount()) {
3318             runningaccountmap.insert(n, n->runningAccount()->name());
3319             n->setRunningAccount(0);
3320         }
3321         if (n->estimate()->calendar()) {
3322             nodecalendarmap.insert(n, n->estimate()->calendar()->id());
3323             n->estimate()->setCalendar(0);
3324         }
3325     }
3326     // get resources pointing to calendars and accounts
3327     QHash<Resource*, QString> resaccountmap;
3328     QHash<Resource*, QString> rescalendarmap;
3329     foreach (Resource *r, project.resourceList()) {
3330         if (r->account()) {
3331             resaccountmap.insert(r, r->account()->name());
3332             r->setAccount(0);
3333         }
3334         if (r->calendar()) {
3335             rescalendarmap.insert(r, r->calendar()->id());
3336             r->setCalendar(0);
3337         }
3338     }
3339     // create add account commands and keep track of used and unused accounts
3340     QList<Account*> unusedAccounts;
3341     QMap<QString, Account*> accountsmap;
3342     foreach (Account *a,  m_project->accounts().allAccounts()) {
3343         accountsmap.insert(a->name(), a);
3344     }
3345     foreach (Account *a, project.accounts().accountList()) {
3346         addAccounts(a, 0, unusedAccounts, accountsmap);
3347     }
3348     // create add calendar commands and keep track of used and unused calendars
3349     QList<Calendar*> unusedCalendars;
3350     QMap<QString, Calendar*> calendarsmap;
3351     foreach (Calendar *c,  m_project->allCalendars()) {
3352         calendarsmap.insert(c->id(), c);
3353     }
3354     foreach (Calendar *c, project.calendars()) {
3355         addCalendars(c, 0, unusedCalendars, calendarsmap);
3356     }
3357     // get all requests before resources are merged
3358     QHash<ResourceGroupRequest*, QPair<Node *, ResourceGroup*> > greqs;
3359     QHash<ResourceGroupRequest*, QPair<ResourceRequest*, Resource*> > rreqs;
3360     foreach (Node *n, project.allNodes()) {
3361         QList<ResourceRequest*> resReq;
3362         if (n->type() != (int)Node::Type_Task || n->requests().isEmpty()) {
3363             continue;
3364         }
3365         while (ResourceGroupRequest *gr = n->requests().requests().value(0)) {
3366             while (ResourceRequest *rr = gr->resourceRequests(false).value(0)) {
3367                 debugPlanInsertProject<<"Get resource request:"<<rr;
3368                 rreqs.insertMulti(gr, QPair<ResourceRequest*, Resource*>(rr, rr->resource()));
3369                 // all resource requests shall be reinserted
3370                 rr->unregisterRequest();
3371                 gr->takeResourceRequest(rr);
3372             }
3373             // all group requests shall be reinserted
3374             greqs[ gr ] = QPair<Node*, ResourceGroup*>(n, gr->group());
3375             gr->group()->unregisterRequest(gr);
3376             int i = n->requests().takeRequest(gr);
3377             Q_ASSERT(i >= 0);
3378 #ifdef NDEBUG
3379             Q_UNUSED(i);
3380 #endif
3381         }
3382     }
3383     QList<ResourceGroup*> allGroups;
3384     QList<Resource*> allResources;
3385     QList<Resource*> newResources;
3386     QHash<ResourceGroup*, ResourceGroup*> existingGroups;
3387     QHash<Resource*, Resource*> existingResources;
3388     foreach (ResourceGroup *g, project.resourceGroups()) {
3389         ResourceGroup *gr = m_project->findResourceGroup(g->id());
3390         if (gr == 0) {
3391             addCommand(new AddResourceGroupCmd(m_project, g, kundo2_noi18n("ResourceGroup")));
3392             gr = g;
3393             debugPlanInsertProject<<"AddResourceGroupCmd:"<<gr->name();
3394         } else {
3395             existingGroups[ gr ] = g;
3396         }
3397         allGroups << gr;
3398         foreach (Resource *r, g->resources()) {
3399             while (Schedule *s = r->schedules().values().value(0)) {
3400                 r->deleteSchedule(s); // schedules not handled
3401             }
3402             Resource *res = m_project->findResource(r->id());
3403             if (res == 0) {
3404                 addCommand(new AddResourceCmd(gr, r, kundo2_noi18n("Resource")));
3405                 allResources << r;
3406                 newResources << r;
3407                 debugPlanInsertProject<<"AddResourceCmd:"<<gr->name()<<r->name();
3408             } else {
3409                 existingResources[ res ] = r;
3410                 allResources << res;
3411             }
3412         }
3413     }
3414     // Update resource account
3415     {QHash<Resource*, QString>::const_iterator it = resaccountmap.constBegin();
3416     QHash<Resource*, QString>::const_iterator end = resaccountmap.constEnd();
3417     for (; it != end; ++it) {
3418         Resource *r = it.key();
3419         if (newResources.contains(r)) {
3420             Q_ASSERT(allResources.contains(r));
3421             addCommand(new ResourceModifyAccountCmd(*r, 0, accountsmap.value(it.value())));
3422         }
3423     }}
3424     // Update resource calendar
3425     {QHash<Resource*, QString>::const_iterator it = rescalendarmap.constBegin();
3426     QHash<Resource*, QString>::const_iterator end = rescalendarmap.constEnd();
3427     for (; it != end; ++it) {
3428         Resource *r = it.key();
3429         if (newResources.contains(r)) {
3430             Q_ASSERT(allResources.contains(r));
3431             addCommand(new ModifyResourceCalendarCmd(r, calendarsmap.value(it.value())));
3432         }
3433     }}
3434     // Requests: clean up requests to resources already in m_project
3435     int gi = 0;
3436     int ri = 0;
3437     QHash<ResourceGroupRequest*, QPair<Node *, ResourceGroup*> >::const_iterator gregsIt;
3438     for (gregsIt = greqs.constBegin(); gregsIt != greqs.constEnd(); ++gregsIt) {
3439         ResourceGroupRequest *gr = gregsIt.key();
3440         QPair<Node*, ResourceGroup*> pair = gregsIt.value();
3441         Node *n = pair.first;
3442         ResourceGroup *newGroup = pair.second;
3443         if (ResourceGroup *ng = existingGroups.key(newGroup)) {
3444             newGroup = ng;
3445             debugPlanInsertProject<<"Using existing group:"<<newGroup<<newGroup->requests();
3446         } else { debugPlanInsertProject<<"Using group from inserted project:"<<newGroup<<newGroup->requests(); }
3447         Q_ASSERT(allGroups.contains(newGroup));
3448         gr->setGroup(newGroup);
3449         addCommand(new AddResourceGroupRequestCmd(static_cast<Task&>(*n), gr, kundo2_noi18n("Group %1", ++gi)));
3450         debugPlanInsertProject<<"Add resource group request:"<<n->name()<<":"<<newGroup->name()<<"requests:"<<newGroup->requests();
3451         QHash<ResourceGroupRequest*, QPair<ResourceRequest*, Resource*> >::const_iterator i = rreqs.constFind(gr);
3452         for (; i != rreqs.constEnd() && i.key() == gr; ++i) {
3453             ResourceRequest *rr = i.value().first;
3454             Resource *newRes = i.value().second;
3455             if (Resource *nr = existingResources.key(newRes)) {
3456                 newRes = nr;
3457             }
3458             debugPlanInsertProject<<"Add resource request:"<<n->name()<<":"<<newGroup->name()<<":"<<newRes->name();
3459             if (! rr->requiredResources().isEmpty()) {
3460                 // the resource request may have required resources that needs mapping
3461                 QList<Resource*> required;
3462                 foreach (Resource *r, rr->requiredResources()) {
3463                     if (newResources.contains(r)) {
3464                         required << r;
3465                         debugPlanInsertProject<<"Request: required (new)"<<r->name();
3466                         continue;
3467                     }
3468                     Resource *r2 = existingResources.key(r);
3469                     Q_ASSERT(allResources.contains(r2));
3470                     if (r2) {
3471                         debugPlanInsertProject<<"Request: required (existing)"<<r2->name();
3472                         required << r2;
3473                     }
3474                 }
3475                 rr->setRequiredResources(required);
3476             }
3477             Q_ASSERT(allResources.contains(newRes));
3478             // all resource requests shall be reinserted
3479             rr->setResource(newRes);
3480             addCommand(new AddResourceRequestCmd(gr, rr, kundo2_noi18n("Resource %1", ++ri)));
3481         }
3482     }
3483     // Add nodes (ids are unique, no need to check)
3484     Node *node_after = after;
3485     for (int i = 0; i < project.numChildren(); ++i) {
3486         Node *n = project.childNode(i);
3487         Q_ASSERT(n);
3488         while (Schedule *s = n->schedules().values().value(0)) {
3489             n->takeSchedule(s); // schedules not handled
3490             delete s;
3491         }
3492         n->setParentNode(0);
3493         if (node_after) {
3494             addCommand(new TaskAddCmd(m_project, n, node_after, kundo2_noi18n("Task")));
3495             node_after = n;
3496         } else {
3497             addCommand(new SubtaskAddCmd(m_project, n, parent, kundo2_noi18n("Subtask")));
3498         }
3499         addChildNodes(n);
3500     }
3501     // Dependencies:
3502     foreach (Node *n, project.allNodes()) {
3503         while (n->numDependChildNodes() > 0) {
3504             Relation *r = n->dependChildNodes().at(0);
3505             n->takeDependChildNode(r);
3506             r->child()->takeDependParentNode(r);
3507             addCommand(new AddRelationCmd(*m_project, r));
3508         }
3509     }
3510     // node calendar
3511     {QHash<Node*, QString>::const_iterator it = nodecalendarmap.constBegin();
3512     QHash<Node*, QString>::const_iterator end = nodecalendarmap.constEnd();
3513     for (; it != end; ++it) {
3514         addCommand(new ModifyEstimateCalendarCmd(*(it.key()), 0, calendarsmap.value(it.value())));
3515     }}
3516     // node startup account
3517     {QHash<Node*, QString>::const_iterator it = startupaccountmap.constBegin();
3518     QHash<Node*, QString>::const_iterator end = startupaccountmap.constEnd();
3519     for (; it != end; ++it) {
3520         addCommand(new NodeModifyStartupAccountCmd(*(it.key()), 0, accountsmap.value(it.value())));
3521     }}
3522     // node shutdown account
3523     {QHash<Node*, QString>::const_iterator it = shutdownaccountmap.constBegin();
3524     QHash<Node*, QString>::const_iterator end = shutdownaccountmap.constEnd();
3525     for (; it != end; ++it) {
3526         addCommand(new NodeModifyShutdownAccountCmd(*(it.key()), 0, accountsmap.value(it.value())));
3527     }}
3528     // node running account
3529     {QHash<Node*, QString>::const_iterator it = runningaccountmap.constBegin();
3530     QHash<Node*, QString>::const_iterator end = runningaccountmap.constEnd();
3531     for (; it != end; ++it) {
3532         addCommand(new NodeModifyRunningAccountCmd(*(it.key()), 0, accountsmap.value(it.value())));
3533     }}
3534 
3535     if (! defaultAccount.isEmpty()) {
3536         Account *a = accountsmap.value(defaultAccount);
3537         if (a && a->list()) {
3538             addCommand(new ModifyDefaultAccountCmd(m_project->accounts(), 0, a));
3539         }
3540     }
3541     debugPlanInsertProject<<"Cleanup unused stuff from inserted project:"<<&project;
3542     // Cleanup
3543     // Remove nodes from project so they are not deleted
3544     while (Node *ch = project.childNode(0)) {
3545         project.takeChildNode(ch);
3546     }
3547     foreach (Node *n, project.allNodes()) {
3548         project.removeId(n->id());
3549     }
3550 
3551     // Remove calendars from project
3552     while (project.calendarCount() > 0) {
3553         project.takeCalendar(project.calendarAt(0));
3554     }
3555     qDeleteAll(unusedCalendars);
3556 
3557     // Remove accounts from project
3558     while (project.accounts().accountCount() > 0) {
3559         project.accounts().take(project.accounts().accountAt(0));
3560     }
3561     qDeleteAll(unusedAccounts);
3562 
3563     while (project.numResourceGroups() > 0) {
3564         ResourceGroup *g = project.resourceGroupAt(0);
3565         debugPlanInsertProject<<"Take used group:"<<g<<g->requests();
3566         while (g->numResources() > 0) {
3567             g->takeResource(g->resourceAt(0));
3568         }
3569         project.takeResourceGroup(g);
3570     }
3571     qDeleteAll(existingResources); // deletes unused resources
3572     debugPlanInsertProject<<"Delete unused groups:"<<existingGroups;
3573     qDeleteAll(existingGroups); // deletes unused resource groups
3574 }
3575 
addCalendars(Calendar * calendar,Calendar * parent,QList<Calendar * > & unused,QMap<QString,Calendar * > & calendarsmap)3576 void InsertProjectCmd::addCalendars(Calendar *calendar, Calendar *parent, QList<Calendar*> &unused, QMap<QString, Calendar*> &calendarsmap) {
3577     Calendar *par = 0;
3578     if (parent) {
3579         par = calendarsmap.value(parent->id());
3580     }
3581     if (par == 0) {
3582         par = parent;
3583     }
3584     Calendar *cal = calendarsmap.value(calendar->id());
3585     if (cal == 0) {
3586         calendarsmap.insert(calendar->id(), calendar);
3587         addCommand(new CalendarAddCmd(m_project, calendar, -1, par));
3588     } else {
3589         unused << calendar;
3590     }
3591     foreach (Calendar *c, calendar->calendars()) {
3592         addCalendars(c, calendar, unused, calendarsmap);
3593     }
3594 }
3595 
addAccounts(Account * account,Account * parent,QList<Account * > & unused,QMap<QString,Account * > & accountsmap)3596 void InsertProjectCmd::addAccounts(Account *account, Account *parent, QList<Account*>  &unused, QMap<QString, Account*>  &accountsmap) {
3597     Account *par = 0;
3598     if (parent) {
3599         par = accountsmap.value(parent->name());
3600     }
3601     if (par == 0) {
3602         par = parent;
3603     }
3604     Account *acc = accountsmap.value(account->name());
3605     if (acc == 0) {
3606         debugPlanInsertProject<<"Move to new project:"<<account<<account->name();
3607         accountsmap.insert(account->name(), account);
3608         addCommand(new AddAccountCmd(*m_project, account, par, -1, kundo2_noi18n("Add account %1", account->name())));
3609     } else {
3610         debugPlanInsertProject<<"Already exists:"<<account<<account->name();
3611         unused << account;
3612     }
3613     while (! account->accountList().isEmpty()) {
3614         Account *a = account->accountList().first();
3615         account->list()->take(a);
3616         addAccounts(a, account, unused, accountsmap);
3617     }
3618 }
3619 
addChildNodes(Node * node)3620 void InsertProjectCmd::addChildNodes(Node *node) {
3621     // schedules not handled
3622     while (Schedule *s = node->schedules().values().value(0)) {
3623         node->takeSchedule(s); // schedules not handled
3624         delete s;
3625     }
3626     foreach (Node *n, node->childNodeIterator()) {
3627         n->setParentNode(0);
3628         addCommand(new SubtaskAddCmd(m_project, n, node, kundo2_noi18n("Subtask")));
3629         addChildNodes(n);
3630     }
3631     // Remove child nodes so they are not added twice
3632     while (Node *ch = node->childNode(0)) {
3633         node->takeChildNode(ch);
3634     }
3635 }
3636 
execute()3637 void InsertProjectCmd::execute()
3638 {
3639     QApplication::setOverrideCursor(Qt::WaitCursor);
3640     MacroCommand::execute();
3641     QApplication::restoreOverrideCursor();
3642 }
unexecute()3643 void InsertProjectCmd::unexecute()
3644 {
3645     QApplication::setOverrideCursor(Qt::WaitCursor);
3646     MacroCommand::unexecute();
3647     QApplication::restoreOverrideCursor();
3648 }
3649 
WorkPackageAddCmd(Project * project,Node * node,WorkPackage * value,const KUndo2MagicString & name)3650 WorkPackageAddCmd::WorkPackageAddCmd(Project *project, Node *node, WorkPackage *value, const KUndo2MagicString& name)
3651     : NamedCommand(name),
3652     m_project(project),
3653     m_node(node),
3654     m_wp(value),
3655     m_mine(true)
3656 {
3657 }
~WorkPackageAddCmd()3658 WorkPackageAddCmd::~WorkPackageAddCmd()
3659 {
3660     if (m_mine) {
3661         delete m_wp;
3662     }
3663 }
execute()3664 void WorkPackageAddCmd::execute()
3665 {
3666     // FIXME use project
3667     //m_project->addWorkPackage(m_node, m_wp);
3668     static_cast<Task*>(m_node)->addWorkPackage(m_wp);
3669 }
unexecute()3670 void WorkPackageAddCmd::unexecute()
3671 {
3672     // FIXME use project
3673     //m_project->removeWorkPackage(m_node, m_wp);
3674     static_cast<Task*>(m_node)->removeWorkPackage(m_wp);
3675 }
3676 
ModifyProjectLocaleCmd(Project & project,const KUndo2MagicString & name)3677 ModifyProjectLocaleCmd::ModifyProjectLocaleCmd(Project &project, const KUndo2MagicString& name)
3678     : MacroCommand(name),
3679     m_project(project)
3680 {
3681 };
execute()3682 void ModifyProjectLocaleCmd::execute()
3683 {
3684     MacroCommand::execute();
3685     m_project.emitLocaleChanged();
3686 }
unexecute()3687 void ModifyProjectLocaleCmd::unexecute()
3688 {
3689     MacroCommand::unexecute();
3690     m_project.emitLocaleChanged();
3691 }
3692 
ModifyCurrencySymolCmd(Locale * locale,const QString & value,const KUndo2MagicString & name)3693 ModifyCurrencySymolCmd::ModifyCurrencySymolCmd(Locale *locale, const QString &value, const KUndo2MagicString& name)
3694     : NamedCommand(name),
3695     m_locale(locale),
3696     m_newvalue(value),
3697     m_oldvalue(locale->currencySymbol())
3698 {
3699 };
execute()3700 void ModifyCurrencySymolCmd::execute()
3701 {
3702     m_locale->setCurrencySymbol(m_newvalue);
3703 }
unexecute()3704 void ModifyCurrencySymolCmd::unexecute()
3705 {
3706     m_locale->setCurrencySymbol(m_oldvalue);
3707 }
3708 
ModifyCurrencyFractionalDigitsCmd(Locale * locale,int value,const KUndo2MagicString & name)3709 ModifyCurrencyFractionalDigitsCmd::ModifyCurrencyFractionalDigitsCmd(Locale *locale, int value, const KUndo2MagicString& name)
3710     : NamedCommand(name),
3711     m_locale(locale),
3712     m_newvalue(value),
3713     m_oldvalue(locale->monetaryDecimalPlaces())
3714 {
3715 };
execute()3716 void ModifyCurrencyFractionalDigitsCmd::execute()
3717 {
3718     m_locale->setMonetaryDecimalPlaces(m_newvalue);
3719 }
unexecute()3720 void ModifyCurrencyFractionalDigitsCmd::unexecute()
3721 {
3722     m_locale->setMonetaryDecimalPlaces(m_oldvalue);
3723 }
3724 
AddExternalAppointmentCmd(Resource * resource,const QString & pid,const QString & pname,const QDateTime & start,const QDateTime & end,double load,const KUndo2MagicString & name)3725 AddExternalAppointmentCmd::AddExternalAppointmentCmd(Resource *resource, const QString &pid, const QString &pname, const QDateTime &start, const QDateTime &end, double load, const KUndo2MagicString& name)
3726     : NamedCommand(name),
3727     m_resource(resource),
3728     m_pid(pid),
3729     m_pname(pname),
3730     m_start(start),
3731     m_end(end),
3732     m_load(load)
3733 {
3734 }
3735 
execute()3736 void AddExternalAppointmentCmd::execute()
3737 {
3738     m_resource->addExternalAppointment(m_pid, m_pname, m_start, m_end, m_load);
3739 }
3740 
unexecute()3741 void AddExternalAppointmentCmd::unexecute()
3742 {
3743     m_resource->subtractExternalAppointment(m_pid, m_start, m_end, m_load);
3744     // FIXME do this smarter
3745     if (! m_resource->externalAppointments(m_pid).isEmpty()) {
3746         m_resource->takeExternalAppointment(m_pid);
3747     }
3748 }
3749 
ClearExternalAppointmentCmd(Resource * resource,const QString & pid,const KUndo2MagicString & name)3750 ClearExternalAppointmentCmd::ClearExternalAppointmentCmd(Resource *resource, const QString &pid, const KUndo2MagicString &name)
3751     : NamedCommand(name),
3752     m_resource(resource),
3753     m_pid(pid),
3754     m_appointments(0)
3755 {
3756 }
3757 
~ClearExternalAppointmentCmd()3758 ClearExternalAppointmentCmd::~ClearExternalAppointmentCmd()
3759 {
3760     delete m_appointments;
3761 }
3762 
execute()3763 void ClearExternalAppointmentCmd::execute()
3764 {
3765 //     debugPlan<<text()<<":"<<m_resource->name()<<m_pid;
3766     m_appointments = m_resource->takeExternalAppointment(m_pid);
3767 }
3768 
unexecute()3769 void ClearExternalAppointmentCmd::unexecute()
3770 {
3771 //     debugPlan<<text()<<":"<<m_resource->name()<<m_pid;
3772     if (m_appointments) {
3773         m_resource->addExternalAppointment(m_pid, m_appointments);
3774     }
3775     m_appointments = 0;
3776 }
3777 
ClearAllExternalAppointmentsCmd(Project * project,const KUndo2MagicString & name)3778 ClearAllExternalAppointmentsCmd::ClearAllExternalAppointmentsCmd(Project *project, const KUndo2MagicString &name)
3779     : NamedCommand(name),
3780     m_project(project)
3781 {
3782     foreach (Resource *r, project->resourceList()) {
3783         const QMap<QString, QString> map = r->externalProjects();
3784         QMap<QString, QString>::const_iterator it;
3785         for (it = map.constBegin(); it != map.constEnd(); ++it) {
3786             m_cmd.addCommand(new ClearExternalAppointmentCmd(r, it.key()));
3787         }
3788     }
3789 }
3790 
execute()3791 void ClearAllExternalAppointmentsCmd::execute()
3792 {
3793     m_cmd.redo();
3794 }
3795 
unexecute()3796 void ClearAllExternalAppointmentsCmd::unexecute()
3797 {
3798     m_cmd.undo();
3799 }
3800 
SharedResourcesFileCmd(Project * project,const QString & newValue,const KUndo2MagicString & name)3801 SharedResourcesFileCmd::SharedResourcesFileCmd(Project *project, const QString &newValue, const KUndo2MagicString& name)
3802     : NamedCommand(name)
3803     , m_project(project)
3804     , m_oldValue(project->sharedResourcesFile())
3805     , m_newValue(newValue)
3806 {
3807 }
3808 
execute()3809 void SharedResourcesFileCmd::execute()
3810 {
3811     m_project->setSharedResourcesFile(m_newValue);
3812 }
3813 
unexecute()3814 void SharedResourcesFileCmd::unexecute()
3815 {
3816     m_project->setSharedResourcesFile(m_oldValue);
3817 }
3818 
UseSharedResourcesCmd(Project * project,bool newValue,const KUndo2MagicString & name)3819 UseSharedResourcesCmd::UseSharedResourcesCmd(Project *project, bool newValue, const KUndo2MagicString& name)
3820     : NamedCommand(name)
3821     , m_project(project)
3822     , m_oldValue(project->useSharedResources())
3823     , m_newValue(newValue)
3824 {
3825 }
3826 
execute()3827 void UseSharedResourcesCmd::execute()
3828 {
3829     m_project->setUseSharedResources(m_newValue);
3830 }
3831 
unexecute()3832 void UseSharedResourcesCmd::unexecute()
3833 {
3834     m_project->setUseSharedResources(m_oldValue);
3835 }
3836 
SharedProjectsUrlCmd(Project * project,const QUrl & newValue,const KUndo2MagicString & name)3837 SharedProjectsUrlCmd::SharedProjectsUrlCmd(Project *project, const QUrl &newValue, const KUndo2MagicString& name)
3838     : NamedCommand(name)
3839     , m_project(project)
3840     , m_oldValue(project->sharedProjectsUrl())
3841     , m_newValue(newValue)
3842 {
3843 }
3844 
execute()3845 void SharedProjectsUrlCmd::execute()
3846 {
3847     m_project->setSharedProjectsUrl(m_newValue);
3848 }
3849 
unexecute()3850 void SharedProjectsUrlCmd::unexecute()
3851 {
3852     m_project->setSharedProjectsUrl(m_oldValue);
3853 }
3854 
LoadProjectsAtStartupCmd(Project * project,bool newValue,const KUndo2MagicString & name)3855 LoadProjectsAtStartupCmd::LoadProjectsAtStartupCmd(Project *project, bool newValue, const KUndo2MagicString& name)
3856     : NamedCommand(name)
3857     , m_project(project)
3858     , m_oldValue(project->loadProjectsAtStartup())
3859     , m_newValue(newValue)
3860 {
3861 }
3862 
execute()3863 void LoadProjectsAtStartupCmd::execute()
3864 {
3865     m_project->setLoadProjectsAtStartup(m_newValue);
3866 }
3867 
unexecute()3868 void LoadProjectsAtStartupCmd::unexecute()
3869 {
3870     m_project->setLoadProjectsAtStartup(m_oldValue);
3871 }
3872 
3873 }  //KPlato namespace
3874