summaryrefslogtreecommitdiffstats
path: root/kmymoney2/mymoney/mymoneykeyvaluecontainer.cpp
blob: cabf7726dd2bd719fc2100da2c24745078743e9c (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
/***************************************************************************
                          mymoneykeyvaluecontainer.cpp
                             -------------------
    begin                : Sun Nov 10 2002
    copyright            : (C) 2002-2005 by Thomas Baumgart
    email                : Thomas Baumgart <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.                                   *
 *                                                                         *
 ***************************************************************************/

// ----------------------------------------------------------------------------
// QT Includes

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

#include <kmymoney/mymoneykeyvaluecontainer.h>
#include <kmymoney/mymoneyexception.h>

MyMoneyKeyValueContainer::MyMoneyKeyValueContainer()
{
}

MyMoneyKeyValueContainer::MyMoneyKeyValueContainer(const TQDomElement& node)
{
  if(!node.isNull()) {
    if("KEYVALUEPAIRS" != node.tagName())
      throw new MYMONEYEXCEPTION("Node was not KEYVALUEPAIRS");

    m_kvp.clear();

    TQDomNodeList nodeList = node.elementsByTagName("PAIR");
    for(unsigned int i = 0; i < nodeList.count(); ++i) {
      const TQDomElement& el(nodeList.item(i).toElement());
      m_kvp[el.attribute("key")] = el.attribute("value");
    }
  }
}

MyMoneyKeyValueContainer::~MyMoneyKeyValueContainer()
{
}

const TQString& MyMoneyKeyValueContainer::value(const TQString& key) const
{
  TQMap<TQString, TQString>::ConstIterator it;

  it = m_kvp.find(key);
  if(it != m_kvp.end())
    return (*it);
  return TQString::null;
}

void MyMoneyKeyValueContainer::setValue(const TQString& key, const TQString& value)
{
  m_kvp[key] = value;
}


void MyMoneyKeyValueContainer::setPairs(const TQMap<TQString, TQString>& list)
{
  m_kvp = list;
}

void MyMoneyKeyValueContainer::deletePair(const TQString& key)
{
  TQMap<TQString, TQString>::Iterator it;

  it = m_kvp.find(key);
  if(it != m_kvp.end())
    m_kvp.remove(it);
}

void MyMoneyKeyValueContainer::clear(void)
{
  m_kvp.clear();
}

bool MyMoneyKeyValueContainer::operator == (const MyMoneyKeyValueContainer& right) const
{
  TQMap<TQString, TQString>::ConstIterator it_a, it_b;

  it_a = m_kvp.begin();
  it_b = right.m_kvp.begin();

  while(it_a != m_kvp.end() && it_b != right.m_kvp.end()) {
    if(it_a.key() != it_b.key()
    || (((*it_a).length() != 0 || (*it_b).length() != 0) && *it_a != *it_b))
      return false;
    ++it_a;
    ++it_b;
  }

  return (it_a == m_kvp.end() && it_b == right.m_kvp.end());
}

void MyMoneyKeyValueContainer::writeXML(TQDomDocument& document, TQDomElement& parent) const
{
  if(m_kvp.count() != 0) {
    TQDomElement el = document.createElement("KEYVALUEPAIRS");

    TQMap<TQString, TQString>::ConstIterator it;
    for(it = m_kvp.begin(); it != m_kvp.end(); ++it)
    {
      TQDomElement pair = document.createElement("PAIR");
      pair.setAttribute("key", it.key());
      pair.setAttribute("value", it.data());
      el.appendChild(pair);
    }

    parent.appendChild(el);
  }
}