summaryrefslogtreecommitdiffstats
path: root/kmymoney2/mymoney/mymoneyobjectcontainer.cpp
blob: 63f4d1cd83f5e099ffa67470b0b6095696426139 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/***************************************************************************
                          mymoneyobjectcontainer.cpp
                          -------------------
    copyright            : (C) 2006 by Thomas Baumagrt
    email                : 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

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

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

#include <kmymoney/mymoneyobjectcontainer.h>

MyMoneyObjectContainer::MyMoneyObjectContainer()
{
}

MyMoneyObjectContainer::~MyMoneyObjectContainer()
{
  clear();
}

void MyMoneyObjectContainer::clear(IMyMoneyStorage* storage)
{
  // delete all objects
  TQMap<TQString, MyMoneyObject const *>::const_iterator it;
  for(it = m_map.begin(); it != m_map.end(); ++it) {
    delete (*it);
  }

  // then delete the pointers to them
  m_map.clear();

  if(storage)
    m_storage = storage;
}

void MyMoneyObjectContainer::clear(const TQString& id)
{
  TQMap<TQString, MyMoneyObject const *>::iterator it;
  it = m_map.find(id);
  if(it != m_map.end()) {
    delete (*it);
    m_map.erase(it);
  }
}

#define listMethod(a, T) \
void MyMoneyObjectContainer::a(TQValueList<T>& list) \
{ \
  TQMap<TQString, const MyMoneyObject*>::const_iterator it; \
  for(it = m_map.begin(); it != m_map.end(); ++it) { \
    const T* node = dynamic_cast<const T*>(*it); \
    if(node) { \
      list.append(*node); \
    } \
  } \
}

#define preloadListMethod(a, T) \
void MyMoneyObjectContainer::preload##a(const TQValueList<T>& list) \
{ \
  TQValueList<T>::const_iterator it; \
  for(it = list.begin(); it != list.end(); ++it) { \
    delete m_map[(*it).id()]; \
    m_map[(*it).id()] = new T(*it); \
  } \
}

#define preloadMethod(a, T) \
void MyMoneyObjectContainer::preload##a(const T& obj) \
{ \
  delete m_map[obj.id()]; \
  m_map[obj.id()] = new T(obj); \
}

#define objectAccessMethod(a, T) \
const T& MyMoneyObjectContainer::a(const TQString& id) \
{ \
  static T nullElement; \
  if(id.isEmpty()) \
    return nullElement; \
  TQMap<TQString, MyMoneyObject const *>::const_iterator it; \
  it = m_map.find(id); \
  if(it == m_map.end()) { \
    /* not found, need to load from engine */ \
    T x = m_storage->a(id); \
    m_map[id] = new T(x); \
    return dynamic_cast<const T&>(*m_map[id]); \
  } \
  return dynamic_cast<const T&>(*(*it)); \
}

void MyMoneyObjectContainer::account(TQValueList<MyMoneyAccount>& list)
{
  TQMap<TQString, const MyMoneyObject*>::const_iterator it;
  for(it = m_map.begin(); it != m_map.end(); ++it) {
    const MyMoneyAccount* node = dynamic_cast<const MyMoneyAccount*>(*it);
    if(node) {
      assignFraction(const_cast<MyMoneyAccount*>(node));
      list.append(*node);
    }
  }
}

const MyMoneyAccount& MyMoneyObjectContainer::account(const TQString& id)
{
  static MyMoneyAccount nullElement;
  if(id.isEmpty())
    return nullElement;
  TQMap<TQString, MyMoneyObject const *>::iterator it;
  it = m_map.find(id);
  if(it == m_map.end()) {
    /* not found, need to load from engine */
    MyMoneyAccount x = m_storage->account(id);
    MyMoneyAccount* item = new MyMoneyAccount(x);
    assignFraction(dynamic_cast<MyMoneyAccount*>(item));
    m_map[id] = item;
    return dynamic_cast<const MyMoneyAccount&>(*m_map[id]);
  }
  assignFraction(dynamic_cast<MyMoneyAccount*> (const_cast<MyMoneyObject*>(*it)));
  return dynamic_cast<const MyMoneyAccount&>(*(*it));
}

void MyMoneyObjectContainer::assignFraction(MyMoneyAccount* acc)
{
  if(acc != 0 && acc->m_fraction == -1) {
    const MyMoneySecurity& sec = security(acc->currencyId());
    acc->fraction(sec);
  }
}

const MyMoneyAccount& MyMoneyObjectContainer::accountByName(const TQString& name) const
{
  static MyMoneyAccount nullElement;
  TQMap<TQString, MyMoneyObject const *>::const_iterator it;
  for(it = m_map.begin(); it != m_map.end(); ++it) {
    const MyMoneyAccount* node = dynamic_cast<const MyMoneyAccount *>(*it);
    if(node) {
      if(node->name() == name)
        return dynamic_cast<const MyMoneyAccount &>(*(*it));
    }
  }
  return nullElement;
}

void MyMoneyObjectContainer::refresh(const TQString& id)
{
  if(id.isEmpty())
    return;

  TQMap<TQString, MyMoneyObject const *>::const_iterator it;
  it = m_map.find(id);
  if(it != m_map.end()) {
    const MyMoneyAccount* account = dynamic_cast<const MyMoneyAccount *>(*it);
    const MyMoneyPayee* payee = dynamic_cast<const MyMoneyPayee *>(*it);
    const MyMoneySecurity* security = dynamic_cast<const MyMoneySecurity *>(*it);
    const MyMoneyInstitution* institution = dynamic_cast<const MyMoneyInstitution *>(*it);
    const MyMoneySchedule* schedule = dynamic_cast<const MyMoneySchedule *>(*it);
    delete *it;
    if(account) {
      const MyMoneyAccount& a = m_storage->account(id);
      m_map[id] = new MyMoneyAccount(a);
    } else if(security) {
      const MyMoneySecurity& s = m_storage->security(id);
      if(s.id().isEmpty()) {
        const MyMoneySecurity& c = m_storage->currency(id);
        m_map[id] = new MyMoneySecurity(c);
      } else {
        m_map[id] = new MyMoneySecurity(s);
      }
    } else if(payee) {
      const MyMoneyPayee& p = m_storage->payee(id);
      m_map[id] = new MyMoneyPayee(p);
    } else if(institution) {
      const MyMoneyInstitution& i = m_storage->institution(id);
      m_map[id] = new MyMoneyInstitution(i);
    } else if(schedule) {
      const MyMoneySchedule& s = m_storage->schedule(id);
      m_map[id] = new MyMoneySchedule(s);
    } else {
      tqWarning("Ooops, should preload an unknown object with id '%s'", id.data());
    }
    return;
  }
}

objectAccessMethod(schedule, MyMoneySchedule)
objectAccessMethod(payee, MyMoneyPayee)
objectAccessMethod(security, MyMoneySecurity)
objectAccessMethod(institution, MyMoneyInstitution)

preloadListMethod(Account, MyMoneyAccount)
preloadListMethod(Payee, MyMoneyPayee)
preloadListMethod(Institution, MyMoneyInstitution)
preloadListMethod(Security, MyMoneySecurity)
preloadListMethod(Schedule, MyMoneySchedule)

preloadMethod(Account, MyMoneyAccount)
preloadMethod(Security, MyMoneySecurity)
preloadMethod(Payee, MyMoneyPayee)
preloadMethod(Institution, MyMoneyInstitution)

listMethod(payee, MyMoneyPayee)
listMethod(institution, MyMoneyInstitution)

#include "mymoneyobjectcontainer.moc"