summaryrefslogtreecommitdiffstats
path: root/kmail/kmmsglist.cpp
blob: 9689467b4162523b3ab4cb4554ba8d08cda06ea6 (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
// kmmsglist.cpp

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

#include "kmmsglist.h"
#include "kmmsgdict.h" // FIXME Till - move those into kmfolderindex
#include "kmkernel.h"
#include <assert.h>
#include <stdlib.h>

//-----------------------------------------------------------------------------
KMMsgList::KMMsgList(int initSize)
  : TQMemArray<KMMsgBase*>(initSize),
    mHigh( 0 ), mCount( 0 )
{
  if ( size() > 0 )
    for (unsigned int i=size(); i>0; i--)
	TQMemArray<KMMsgBase*>::at(i-1) = 0;
}


//-----------------------------------------------------------------------------
KMMsgList::~KMMsgList()
{
  clear(TRUE);
}


//-----------------------------------------------------------------------------
void KMMsgList::clear(bool doDelete, bool syncDict)
{
  if ( mHigh > 0 )
    for (unsigned int i=mHigh; i>0; i--)
    {
      KMMsgBase * msg = at(i-1);
      if (msg) {
        if ( syncDict )
          KMMsgDict::mutableInstance()->remove(msg);
        at(i-1) = 0;
        if (doDelete) delete msg;
      }
    }
  mHigh  = 0;
  mCount = 0;
}


//-----------------------------------------------------------------------------
bool KMMsgList::resize(unsigned int aSize)
{
  unsigned int i, oldSize = size();
  KMMsgBase* msg;

  // delete messages that will get lost, if any
  if (aSize < mHigh)
  {
    for (i=aSize; i<mHigh; i++)
    {
      msg = at(i);
      if (msg)
      {
	delete msg;
	mCount--;
      }
      mHigh = aSize;
    }
  }

  // do the resizing
  if (!TQMemArray<KMMsgBase*>::resize(aSize)) return FALSE;

  // initialize new elements
  for (i=oldSize; i<aSize; i++)
    at(i) = 0;

  return TRUE;
}


//-----------------------------------------------------------------------------
bool KMMsgList::reset(unsigned int aSize)
{
  if (!resize(aSize)) return FALSE;
  clear();
  return TRUE;
}


//-----------------------------------------------------------------------------
void KMMsgList::set(unsigned int idx, KMMsgBase* aMsg)
{
  if (idx >= size())
    resize( idx > 2 * size() ? idx + 16 : 2 * size() );

  if (!at(idx) && aMsg) mCount++;
  else if (at(idx) && !aMsg) mCount--;

  at(idx) = aMsg;

  if (!aMsg || idx >= mHigh) rethinkHigh();
}


//-----------------------------------------------------------------------------
void KMMsgList::insert(unsigned int idx, KMMsgBase* aMsg, bool syncDict)
{
  if (idx >= size())
    resize( idx > 2 * size() ? idx + 16 : 2 * size() );

  if (aMsg) mCount++;

  for (unsigned int i=mHigh; i>idx; i--) {
    if ( syncDict )
      KMMsgDict::mutableInstance()->remove(at(i - 1));
    at(i) = at(i-1);
    if ( syncDict )
      KMMsgDict::mutableInstance()->insert(at(i), i);
  }

  at(idx) = aMsg;
  if ( syncDict )
    KMMsgDict::mutableInstance()->insert(at(idx), idx);

  mHigh++;
}


//-----------------------------------------------------------------------------
unsigned int KMMsgList::append(KMMsgBase* aMsg, bool syncDict)
{
  const unsigned int idx = mHigh;
  insert(idx, aMsg, syncDict); // mHigh gets modified in here
  return idx;
}


//-----------------------------------------------------------------------------
void KMMsgList::remove(unsigned int idx)
{
  assert(idx<size());
  if (at(idx)) {
    mCount--;
    KMMsgDict::mutableInstance()->remove(at(idx));
  }

  mHigh--;
  for (unsigned int i=idx; i<mHigh; i++) {
    KMMsgDict::mutableInstance()->update(at(i + 1), i + 1, i);
    at(i) = at(i+1);
  }

  at(mHigh) = 0;

  rethinkHigh();
}


//-----------------------------------------------------------------------------
KMMsgBase* KMMsgList::take(unsigned int idx)
{
  KMMsgBase* msg=at(idx);
  remove(idx);
  return msg;
}


//-----------------------------------------------------------------------------
void KMMsgList::rethinkHigh()
{
  unsigned int sz = size();

  if (mHigh < sz && at(mHigh))
  {
    // forward search
    while (mHigh < sz && at(mHigh))
      mHigh++;
  }
  else
  {
    // backward search
    while (mHigh>0 && !at(mHigh-1))
      mHigh--;
  }
}