summaryrefslogtreecommitdiffstats
path: root/kmymoney2/mymoney/mymoneykeyvaluecontainer.h
blob: 42169d57c6ccebfc9b147816d4d1a589d35990bb (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
/***************************************************************************
                          mymoneykeyvaluecontainer.h
                             -------------------
    begin                : Sun Nov 10 2002
    copyright            : (C) 2000-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.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef MYMONEYKEYVALUECONTAINER_H
#define MYMONEYKEYVALUECONTAINER_H


/**
  * @author Thomas Baumgart
  */

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

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

#include <tqstring.h>
#include <tqmap.h>
#include <tqdom.h>
#include <kmymoney/export.h>

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


/**
  * This class implements a container for key/value pairs. This is used
  * to store an arbitrary number of attributes with any of the engine
  * object. The container can also be used to have attributes that are
  * attached to this object only for a limited time (e.g. between
  * start of reconciliation end it's end).
  *
  * To give any class the ability to have a key/value pair container,
  * just derive the class from this one. See MyMoneyAccount as an example.
  */
class KMYMONEY_EXPORT MyMoneyKeyValueContainer
{
public:
  MyMoneyKeyValueContainer();
  MyMoneyKeyValueContainer(const TQDomElement& node);

  ~MyMoneyKeyValueContainer();

  /**
    * This method can be used to retrieve the value for a specific @p key.
    * If the key is unknown in this container, an empty string will be returned.
    *
    * @param key const reference to TQString with the key to search for
    * @return reference to value of this key. If the key does not exist,
    *         an emtpy string is returned.
    */
  const TQString& value(const TQString& key) const;

  /**
    * This method is used to add a key/value pair to the container or
    * modify an existing pair.
    *
    * @param key const reference to TQString with the key to search for
    * @param value const reference to TQString with the value for this key
    */
  void setValue(const TQString& key, const TQString& value);

  /**
    * This method is used to remove an existing key/value pair from the
    * container. If the key does not exist, the container is not changed.
    *
    * @param key const reference to TQString with the key to remove
    */
  void deletePair(const TQString& key);

  /**
    * This method clears all pairs currently in the container.
    */
  void clear(void);

  /**
    * This method is used to retrieve the whole set of key/value pairs
    * from the container. It is meant to be used for permanent storage
    * functionality.
    *
    * @return TQMap<TQString, TQString> containing all key/value pairs of
    *         this container.
    */
  const TQMap<TQString, TQString>& pairs(void) const { return m_kvp; };

  /**
    * This method is used to initially store a set of key/value pairs
    * in the container. It is meant to be used for loading functionality
    * from permanent storage.
    *
    * @param list const TQMap<TQString, TQString> containing the set of
    *             key/value pairs to be loaded into the container.
    *
    * @note All existing key/value pairs in the container will be deleted.
    */
  void setPairs(const TQMap<TQString, TQString>& list);

  /**
    * This operator tests for equality of two MyMoneyKeyValueContainer objects
    */
  bool operator == (const MyMoneyKeyValueContainer &) const;

  const TQString& operator[] ( const TQString& k ) const { return value(k); }

  TQString& operator[] ( const TQString& k) { return m_kvp[k]; }

  /**
    * This method creates a TQDomElement for the @p document
    * under the tqparent node @p tqparent.
    *
    * @param document reference to TQDomDocument
    * @param tqparent reference to TQDomElement tqparent node
    */
  void writeXML(TQDomDocument& document, TQDomElement& tqparent) const;

private:
  /**
    * This member variable represents the container of key/value pairs.
    */
  TQMap<TQString, TQString>  m_kvp;
};

#endif