summaryrefslogtreecommitdiffstats
path: root/kmymoney2/mymoney/mymoneyinstitution.cpp
blob: 9c240b54cf05d8b6a5461e94eb0fde1480e353bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/***************************************************************************
                          mymoneyinstitution.cpp
                          -------------------
    copyright            : (C) 2001 by Michael Edwardes,
                               2002-2005 by Thomas Baumgart
    email                : mte@users.sourceforge.net,
                           ipwizard@users.sourceforge.net
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

// ----------------------------------------------------------------------------
// TQt Includes

// ----------------------------------------------------------------------------
// TDE Includes
#include <tdeglobal.h>
#include <kstandarddirs.h>

// ----------------------------------------------------------------------------
// Project Includes

#include "mymoneyinstitution.h"
#include <kmymoney/mymoneyexception.h>

MyMoneyInstitution::MyMoneyInstitution()
{
}

MyMoneyInstitution::MyMoneyInstitution(const TQString& id, const MyMoneyInstitution& right) :
  MyMoneyObject(id)
{
  *this = right;
  m_id = id;
}

MyMoneyInstitution::MyMoneyInstitution(const TQString& name,
                         const TQString& town,
                         const TQString& street,
                         const TQString& postcode,
                         const TQString& telephone,
                         const TQString& manager,
                         const TQString& sortcode)
{
  clearId();
  m_name = name;
  m_town = town;
  m_street = street;
  m_postcode = postcode;
  m_telephone = telephone;
  m_manager = manager;
  m_sortcode = sortcode;
}

MyMoneyInstitution::MyMoneyInstitution(const TQDomElement& node) :
  MyMoneyObject(node),
  MyMoneyKeyValueContainer(node.elementsByTagName("KEYVALUEPAIRS").item(0).toElement())
{
  if("INSTITUTION" != node.tagName())
    throw new MYMONEYEXCEPTION("Node was not INSTITUTION");

  m_sortcode = node.attribute("sortcode");
  m_name = node.attribute("name");
  m_manager = node.attribute("manager");

  TQDomNodeList nodeList = node.elementsByTagName("ADDRESS");
  if(nodeList.count() == 0) {
    TQString msg = TQString("No ADDRESS in institution %1").arg(m_name);
    throw new MYMONEYEXCEPTION(msg);
  }

  TQDomElement addrNode = nodeList.item(0).toElement();
  m_street = addrNode.attribute("street");
  m_town = addrNode.attribute("city");
  m_postcode = addrNode.attribute("zip");
  m_telephone = addrNode.attribute("telephone");

  m_accountList.clear();

  nodeList = node.elementsByTagName("ACCOUNTIDS");
  if(nodeList.count() > 0) {
    nodeList = nodeList.item(0).toElement().elementsByTagName("ACCOUNTID");
    for(unsigned int i = 0; i < nodeList.count(); ++i) {
      m_accountList << nodeList.item(i).toElement().attribute("id");
    }
  }
}

MyMoneyInstitution::~MyMoneyInstitution()
{
}

void MyMoneyInstitution::addAccountId(const TQString& account)
{
  // only add this account if it is not yet presently in the list
  if(m_accountList.contains(account) == 0)
    m_accountList.append(account);
}

TQString MyMoneyInstitution::removeAccountId(const TQString& account)
{
  TQStringList::Iterator pos;
  TQString rc;

  pos = m_accountList.find(account);
  if(pos != m_accountList.end()) {
    m_accountList.remove(pos);
    rc = account;
  }
  return rc;
}

bool MyMoneyInstitution::operator < (const MyMoneyInstitution& right) const
{
  return m_name < right.m_name;
}

bool MyMoneyInstitution::operator == (const MyMoneyInstitution& right) const
{
  if (MyMoneyObject::operator==(right) &&
      ((m_name.length() == 0 && right.m_name.length() == 0) || (m_name == right.m_name)) &&
      ((m_town.length() == 0 && right.m_town.length() == 0) || (m_town == right.m_town)) &&
      ((m_street.length() == 0 && right.m_street.length() == 0) || (m_street == right.m_street)) &&
      ((m_postcode.length() == 0 && right.m_postcode.length() == 0) || (m_postcode == right.m_postcode)) &&
      ((m_telephone.length() == 0 && right.m_telephone.length() == 0) || (m_telephone == right.m_telephone)) &&
      ((m_sortcode.length() == 0 && right.m_sortcode.length() == 0) || (m_sortcode == right.m_sortcode)) &&
      ((m_manager.length() == 0 && right.m_manager.length() == 0) || (m_manager == right.m_manager)) &&
      (m_accountList == right.m_accountList) ) {
    return true;
  } else
    return false;
}

void MyMoneyInstitution::writeXML(TQDomDocument& document, TQDomElement& parent) const
{
  TQDomElement el = document.createElement("INSTITUTION");

  writeBaseXML(document, el);

  el.setAttribute("name", m_name);
  el.setAttribute("manager", m_manager);
  el.setAttribute("sortcode", m_sortcode);

  TQDomElement address = document.createElement("ADDRESS");
  address.setAttribute("street", m_street);
  address.setAttribute("city", m_town);
  address.setAttribute("zip", m_postcode);
  address.setAttribute("telephone", m_telephone);
  el.appendChild(address);


  TQDomElement accounts = document.createElement("ACCOUNTIDS");
  for(TQStringList::ConstIterator it = accountList().begin(); it != accountList().end(); ++it){
    TQDomElement temp = document.createElement("ACCOUNTID");
    temp.setAttribute("id", (*it));
    accounts.appendChild(temp);
  }
  el.appendChild(accounts);

  //Add in Key-Value Pairs for institutions.
  MyMoneyKeyValueContainer::writeXML(document, el);

  parent.appendChild(el);
}

bool MyMoneyInstitution::hasReferenceTo(const TQString& /* id */) const
{
  bool rc = false;
  return rc;
}

TQPixmap MyMoneyInstitution::pixmap() const {
  return TQPixmap(TDEGlobal::dirs()->findResource("appdata",TQString( "icons/hicolor/22x22/actions/%1.png").arg("bank")));
}