/* This file is part of the KDE project Copyright (C) 2005 Dag Andersen This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; version 2 of the License. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include #include #include #include #include "kptaccount.h" #include "kptduration.h" #include "kptproject.h" namespace KPlato { Account::Account() : m_name(), m_description(), m_list(0), m_parent(0), m_accountList(), m_costPlaces() { m_accountList.setAutoDelete(true); m_costPlaces.setAutoDelete(true); } Account::Account(TQString name, TQString description) : m_name(name), m_description(description), m_list(0), m_parent(0), m_accountList(), m_costPlaces() { m_accountList.setAutoDelete(true); m_costPlaces.setAutoDelete(true); } Account::~Account() { m_accountList.clear(); if (findAccount() == this) { removeId(); // only remove myself (I may be just a working copy) } if (m_list) m_list->accountDeleted(this); } void Account::setName(TQString name) { if (findAccount() == this) { removeId(); } m_name = name; insertId(); } void Account::append(Account *account){ Q_ASSERT(account); m_accountList.append(account); account->setList(m_list); account->setParent(this); insertId(account); } void Account::insertChildren() { AccountListIterator it = m_accountList; for (; it.current(); ++it) { it.current()->setList(m_list); it.current()->setParent(this); insertId(it.current()); it.current()->insertChildren(); } } void Account::take(Account *account) { if (account == 0) { return; } if (account->parent() == this) { m_accountList.take(m_accountList.findRef(account)); } else if (account->parent()) { account->parent()->take(account); } else { m_list->take(account); } //kdDebug()<name()<load(e, project)) { append(child); } else { delete child; } } else if (e.tagName() == "account") { Account *child = new Account(); if (child->load(e, project)) { m_accountList.append(child); } else { // TODO: Complain about this kdWarning()< cit = m_costPlaces; for (; cit.current(); ++cit) { cit.current()->save(me); } AccountListIterator it = m_accountList; for (; it.current(); ++it) { it.current()->save(me); } } Account::CostPlace *Account::findCostPlace(const Node &node) const { TQPtrListIterator it = m_costPlaces; for (; it.current(); ++it) { if (&node == it.current()->node()) { return it.current(); } } return 0; } Account::CostPlace *Account::findRunning(const Node &node) const { Account::CostPlace *cp = findCostPlace(node); return cp && cp->running() ? cp : 0; } void Account::removeRunning(const Node &node) { Account::CostPlace *cp = findRunning(node); if (cp) { cp->setRunning(false); if (cp->isEmpty()) { m_costPlaces.removeRef(cp); } } } void Account::addRunning(Node &node) { Account::CostPlace *cp = findCostPlace(node); if (cp) { cp->setRunning(true); return; } append(new CostPlace(this, &node, true)); } Account::CostPlace *Account::findStartup(const Node &node) const { Account::CostPlace *cp = findCostPlace(node); return cp && cp->startup() ? cp : 0; } void Account::removeStartup(const Node &node) { Account::CostPlace *cp = findStartup(node); if (cp) { cp->setStartup(false); if (cp->isEmpty()) { m_costPlaces.removeRef(cp); } } } void Account::addStartup(Node &node) { Account::CostPlace *cp = findCostPlace(node); if (cp) { cp->setStartup(true); return; } append(new CostPlace(this, &node, false, true)); } Account::CostPlace *Account::findShutdown(const Node &node) const { Account::CostPlace *cp = findCostPlace(node); return cp && cp->shutdown() ? cp : 0; } void Account::removeShutdown(const Node &node) { Account::CostPlace *cp = findShutdown(node); if (cp) { cp->setShutdown(false); if (cp->isEmpty()) { m_costPlaces.removeRef(cp); } } } void Account::addShutdown(Node &node) { Account::CostPlace *cp = findCostPlace(node); if (cp) { cp->setShutdown(true); return; } append(new CostPlace(this, &node, false, false, true)); } Account *Account::findAccount(const TQString &id) const { if (m_list) return m_list->findAccount(id); return 0; } bool Account::removeId(const TQString &id) { return (m_list ? m_list->removeId(id) : false); } bool Account::insertId() { return insertId(this); } bool Account::insertId(const Account *account) { return (m_list ? m_list->insertId(account) : false); } //------------------------------------ Account::CostPlace::~CostPlace() { if (m_node) { if (m_running) m_node->setRunningAccount(0); if (m_startup) m_node->setStartupAccount(0); if (m_shutdown) m_node->setShutdownAccount(0); } } void Account::CostPlace::setRunning(bool on ) { m_running = on; if (m_node) m_node->setRunningAccount(on ? m_account : 0); } void Account::CostPlace::setStartup(bool on ) { m_startup = on; if (m_node) m_node->setStartupAccount(on ? m_account : 0); } void Account::CostPlace::setShutdown(bool on ) { m_shutdown = on; if (m_node) m_node->setShutdownAccount(on ? m_account : 0); } bool Account::CostPlace::load(TQDomElement &element, const Project &project) { //kdDebug()< it = account.costPlaces(); for (; it.current(); ++it) { Node *n = it.current()->node(); if (n == 0) { continue; } //kdDebug()<name()<running()) { ec += n->plannedEffortCostPrDay(start, end); } if (it.current()->startup()) { if (n->startTime().date() >= start && n->startTime().date() <= end) ec.add(n->startTime().date(), EffortCost(Duration::zeroDuration, n->startupCost())); } if (it.current()->shutdown()) { if (n->endTime().date() >= start && n->endTime().date() <= end) ec.add(n->endTime().date(), EffortCost(Duration::zeroDuration, n->shutdownCost())); } } if (&account == m_defaultAccount) { TQDictIterator nit = m_project.nodeDict(); for (; nit.current(); ++nit) { Node *n = nit.current(); if (n->runningAccount() == 0) { ec += n->plannedEffortCostPrDay(start, end); } if (n->startupAccount() == 0) { if (n->startTime().date() >= start && n->startTime().date() <= end) ec.add(n->startTime().date(), EffortCost(Duration::zeroDuration, n->startupCost())); } if (n->shutdownAccount() == 0) { if (n->endTime().date() >= start && n->endTime().date() <= end) ec.add(n->endTime().date(), EffortCost(Duration::zeroDuration, n->shutdownCost())); } } } return ec; } void Accounts::append(Account *account) { Q_ASSERT(account); m_accountList.append(account); account->setList(this); account->setParent(0); // incase... insertId(account); //kdDebug()<name()<insertChildren(); } void Accounts::take(Account *account){ if (account == 0) { return; } removeId(account->name()); if (account->parent()) { account->parent()->take(account); return; } m_accountList.take(m_accountList.findRef(account)); //kdDebug()<name()<load(e, project)) { append(child); } else { // TODO: Complain about this kdWarning()<name()); } AccountListIterator it = m_accountList; for (; it.current(); ++it) { it.current()->save(me); } } TQStringList Accounts::costElements() const { TQDictIterator it(m_idDict); TQStringList l; for(; it.current(); ++it) { if (it.current()->isElement()) l << it.currentKey(); } return l; } TQStringList Accounts::nameList() const { TQDictIterator it(m_idDict); TQStringList l; for(; it.current(); ++it) { l << it.currentKey(); } return l; } Account *Accounts::findRunningAccount(const Node &node) const { TQDictIterator it = m_idDict; for (; it.current(); ++it) { if (it.current()->findRunning(node)) return it.current(); } return 0; } Account *Accounts::findStartupAccount(const Node &node) const { TQDictIterator it = m_idDict; for (; it.current(); ++it) { if (it.current()->findStartup(node)) return it.current(); } return 0; } Account *Accounts::findShutdownAccount(const Node &node) const { TQDictIterator it = m_idDict; for (; it.current(); ++it) { if (it.current()->findShutdown(node)) return it.current(); } return 0; } Account *Accounts::findAccount(const TQString &id) const { return m_idDict.find(id); } bool Accounts::insertId(const Account *account) { Q_ASSERT(account); Account *a = m_idDict.find(account->name()); if (a == 0) { //kdDebug()<name()<<"' inserted"<name(), account); return true; } if (a == account) { kdDebug()<name()<<"' allready exists"<