27#include "kptaccount.h"
28#include "kptduration.h"
29#include "kptproject.h"
42 m_accountList.setAutoDelete(
true);
43 m_costPlaces.setAutoDelete(
true);
48 m_description(description),
54 m_accountList.setAutoDelete(
true);
55 m_costPlaces.setAutoDelete(
true);
59 m_accountList.clear();
60 if (findAccount() ==
this) {
64 m_list->accountDeleted(
this);
68void Account::setName(TQString name) {
69 if (findAccount() ==
this) {
76void Account::append(
Account *account){
78 m_accountList.append(account);
79 account->setList(m_list);
80 account->setParent(
this);
84void Account::insertChildren() {
85 AccountListIterator it = m_accountList;
86 for (; it.current(); ++it) {
87 it.current()->setList(m_list);
88 it.current()->setParent(
this);
89 insertId(it.current());
90 it.current()->insertChildren();
94void Account::take(
Account *account) {
98 if (account->parent() ==
this) {
99 m_accountList.take(m_accountList.findRef(account));
100 }
else if (account->parent()) {
101 account->parent()->take(account);
103 m_list->take(account);
108bool Account::load(TQDomElement &element,
const Project &project) {
109 m_name = element.attribute(
"name");
110 m_description = element.attribute(
"description");
111 TQDomNodeList list = element.childNodes();
112 for (
unsigned int i=0; i<list.count(); ++i) {
113 if (list.item(i).isElement()) {
114 TQDomElement e = list.item(i).toElement();
115 if (e.tagName() ==
"costplace") {
116 Account::CostPlace *child =
new Account::CostPlace(
this);
117 if (child->load(e, project)) {
122 }
else if (e.tagName() ==
"account") {
124 if (child->load(e, project)) {
125 m_accountList.append(child);
128 kdWarning()<<k_funcinfo<<
"Loading failed"<<endl;
137void Account::save(TQDomElement &element)
const {
138 TQDomElement me = element.ownerDocument().createElement(
"account");
139 element.appendChild(me);
140 me.setAttribute(
"name", m_name);
141 me.setAttribute(
"description", m_description);
142 TQPtrListIterator<Account::CostPlace> cit = m_costPlaces;
143 for (; cit.current(); ++cit) {
144 cit.current()->save(me);
146 AccountListIterator it = m_accountList;
147 for (; it.current(); ++it) {
148 it.current()->save(me);
152Account::CostPlace *Account::findCostPlace(
const Node &node)
const {
153 TQPtrListIterator<CostPlace> it = m_costPlaces;
154 for (; it.current(); ++it) {
155 if (&node == it.current()->node()) {
162Account::CostPlace *Account::findRunning(
const Node &node)
const {
163 Account::CostPlace *cp = findCostPlace(node);
164 return cp && cp->running() ? cp : 0;
167void Account::removeRunning(
const Node &node) {
168 Account::CostPlace *cp = findRunning(node);
170 cp->setRunning(
false);
172 m_costPlaces.removeRef(cp);
177void Account::addRunning(
Node &node) {
178 Account::CostPlace *cp = findCostPlace(node);
180 cp->setRunning(
true);
183 append(
new CostPlace(
this, &node,
true));
186Account::CostPlace *Account::findStartup(
const Node &node)
const {
187 Account::CostPlace *cp = findCostPlace(node);
188 return cp && cp->startup() ? cp : 0;
191void Account::removeStartup(
const Node &node) {
192 Account::CostPlace *cp = findStartup(node);
194 cp->setStartup(
false);
196 m_costPlaces.removeRef(cp);
201void Account::addStartup(
Node &node) {
202 Account::CostPlace *cp = findCostPlace(node);
204 cp->setStartup(
true);
207 append(
new CostPlace(
this, &node,
false,
true));
211Account::CostPlace *Account::findShutdown(
const Node &node)
const {
212 Account::CostPlace *cp = findCostPlace(node);
213 return cp && cp->shutdown() ? cp : 0;
216void Account::removeShutdown(
const Node &node) {
217 Account::CostPlace *cp = findShutdown(node);
219 cp->setShutdown(
false);
221 m_costPlaces.removeRef(cp);
226void Account::addShutdown(
Node &node) {
227 Account::CostPlace *cp = findCostPlace(node);
229 cp->setShutdown(
true);
232 append(
new CostPlace(
this, &node,
false,
false,
true));
235Account *Account::findAccount(
const TQString &
id)
const {
237 return m_list->findAccount(
id);
241bool Account::removeId(
const TQString &
id) {
242 return (m_list ? m_list->removeId(
id) :
false);
245bool Account::insertId() {
246 return insertId(
this);
249bool Account::insertId(
const Account *account) {
250 return (m_list ? m_list->insertId(account) :
false);
254Account::CostPlace::~CostPlace() {
257 m_node->setRunningAccount(0);
259 m_node->setStartupAccount(0);
261 m_node->setShutdownAccount(0);
265void Account::CostPlace::setRunning(
bool on ) {
268 m_node->setRunningAccount(on ? m_account : 0);
271void Account::CostPlace::setStartup(
bool on ) {
274 m_node->setStartupAccount(on ? m_account : 0);
277void Account::CostPlace::setShutdown(
bool on ) {
280 m_node->setShutdownAccount(on ? m_account : 0);
283bool Account::CostPlace::load(TQDomElement &element,
const Project &project) {
285 m_nodeId = element.attribute(
"node-id");
286 if (m_nodeId.isEmpty()) {
287 kdError()<<k_funcinfo<<
"No node id"<<endl;
290 m_node = project.findNode(m_nodeId);
292 kdError()<<k_funcinfo<<
"Cannot not find node with id: "<<m_nodeId<<endl;
295 setRunning(element.attribute(
"running-cost").toInt());
296 setStartup(element.attribute(
"startup-cost").toInt());
297 setShutdown(element.attribute(
"shutdown-cost").toInt());
301void Account::CostPlace::save(TQDomElement &element)
const {
303 TQDomElement me = element.ownerDocument().createElement(
"costplace");
304 element.appendChild(me);
305 me.setAttribute(
"node-id", m_nodeId);
306 me.setAttribute(
"running-cost", m_running);
307 me.setAttribute(
"startup-cost", m_startup);
308 me.setAttribute(
"shutdown-cost", m_shutdown);
313Accounts::Accounts(Project &project)
314 : m_project(project),
317 m_defaultAccount(0) {
319 m_accountList.setAutoDelete(
true);
322Accounts::~Accounts() {
323 m_accountList.clear();
326EffortCostMap Accounts::plannedCost(
const Account &account,
const TQDate &start,
const TQDate &end) {
328 TQPtrListIterator<Account::CostPlace> it = account.costPlaces();
329 for (; it.current(); ++it) {
330 Node *n = it.current()->node();
335 if (it.current()->running()) {
336 ec += n->plannedEffortCostPrDay(start, end);
338 if (it.current()->startup()) {
339 if (n->startTime().date() >= start &&
340 n->startTime().date() <= end)
343 if (it.current()->shutdown()) {
344 if (n->endTime().date() >= start &&
345 n->endTime().date() <= end)
349 if (&account == m_defaultAccount) {
350 TQDictIterator<Node> nit = m_project.nodeDict();
351 for (; nit.current(); ++nit) {
352 Node *n = nit.current();
353 if (n->runningAccount() == 0) {
354 ec += n->plannedEffortCostPrDay(start, end);
356 if (n->startupAccount() == 0) {
357 if (n->startTime().date() >= start &&
358 n->startTime().date() <= end)
361 if (n->shutdownAccount() == 0) {
362 if (n->endTime().date() >= start &&
363 n->endTime().date() <= end)
371void Accounts::append(
Account *account) {
373 m_accountList.append(account);
374 account->setList(
this);
375 account->setParent(0);
378 account->insertChildren();
381void Accounts::take(
Account *account){
385 removeId(account->name());
386 if (account->parent()) {
387 account->parent()->take(account);
390 m_accountList.take(m_accountList.findRef(account));
394bool Accounts::load(TQDomElement &element,
const Project &project) {
395 TQDomNodeList list = element.childNodes();
396 for (
unsigned int i=0; i<list.count(); ++i) {
397 if (list.item(i).isElement()) {
398 TQDomElement e = list.item(i).toElement();
399 if (e.tagName() ==
"account") {
400 Account *child =
new Account();
401 if (child->load(e, project)) {
405 kdWarning()<<k_funcinfo<<
"Loading failed"<<endl;
411 if (element.hasAttribute(
"default-account")) {
412 m_defaultAccount = findAccount(element.attribute(
"default-account"));
413 if (m_defaultAccount == 0) {
414 kdWarning()<<k_funcinfo<<
"Could not find default account."<<endl;
420void Accounts::save(TQDomElement &element)
const {
421 TQDomElement me = element.ownerDocument().createElement(
"accounts");
422 element.appendChild(me);
423 if (m_defaultAccount) {
424 me.setAttribute(
"default-account", m_defaultAccount->name());
426 AccountListIterator it = m_accountList;
427 for (; it.current(); ++it) {
428 it.current()->save(me);
432TQStringList Accounts::costElements()
const {
433 TQDictIterator<Account> it(m_idDict);
435 for(; it.current(); ++it) {
436 if (it.current()->isElement())
437 l << it.currentKey();
443TQStringList Accounts::nameList()
const {
444 TQDictIterator<Account> it(m_idDict);
446 for(; it.current(); ++it) {
447 l << it.currentKey();
452Account *Accounts::findRunningAccount(
const Node &node)
const {
453 TQDictIterator<Account> it = m_idDict;
454 for (; it.current(); ++it) {
455 if (it.current()->findRunning(node))
461Account *Accounts::findStartupAccount(
const Node &node)
const {
462 TQDictIterator<Account> it = m_idDict;
463 for (; it.current(); ++it) {
464 if (it.current()->findStartup(node))
470Account *Accounts::findShutdownAccount(
const Node &node)
const {
471 TQDictIterator<Account> it = m_idDict;
472 for (; it.current(); ++it) {
473 if (it.current()->findShutdown(node))
479Account *Accounts::findAccount(
const TQString &
id)
const {
480 return m_idDict.find(
id);
483bool Accounts::insertId(
const Account *account) {
485 Account *a = m_idDict.find(account->name());
488 m_idDict.insert(account->name(), account);
492 kdDebug()<<k_funcinfo<<
"'"<<a->name()<<
"' allready exists"<<endl;
496 kdWarning()<<k_funcinfo<<
"Insert failed"<<endl;
500bool Accounts::removeId(
const TQString &
id) {
501 bool res = m_idDict.remove(
id);
507void Accounts::printDebug(TQString ) {
509void Account::printDebug(TQString ) {
Account holds one account.
static const Duration zeroDuration
This is useful for occasions where we need a zero duration.
This class represents any node in the project, a node can be a project or a subproject or any task.
Project is the main node in a project, it contains child nodes and possibly sub-projects.