kmail

kmmsglist.cpp
1 // kmmsglist.cpp
2 
3 #ifdef HAVE_CONFIG_H
4 #include <config.h>
5 #endif
6 
7 #include "kmmsglist.h"
8 #include "kmmsgdict.h" // FIXME Till - move those into kmfolderindex
9 #include "kmkernel.h"
10 #include <assert.h>
11 #include <stdlib.h>
12 
13 //-----------------------------------------------------------------------------
14 KMMsgList::KMMsgList(int initSize)
15  : TQMemArray<KMMsgBase*>(initSize),
16  mHigh( 0 ), mCount( 0 )
17 {
18  if ( size() > 0 )
19  for (unsigned int i=size(); i>0; i--)
20  TQMemArray<KMMsgBase*>::at(i-1) = 0;
21 }
22 
23 
24 //-----------------------------------------------------------------------------
26 {
27  clear(TRUE);
28 }
29 
30 
31 //-----------------------------------------------------------------------------
32 void KMMsgList::clear(bool doDelete, bool syncDict)
33 {
34  if ( mHigh > 0 )
35  for (unsigned int i=mHigh; i>0; i--)
36  {
37  KMMsgBase * msg = at(i-1);
38  if (msg) {
39  if ( syncDict )
40  KMMsgDict::mutableInstance()->remove(msg);
41  at(i-1) = 0;
42  if (doDelete) delete msg;
43  }
44  }
45  mHigh = 0;
46  mCount = 0;
47 }
48 
49 
50 //-----------------------------------------------------------------------------
51 bool KMMsgList::resize(unsigned int aSize)
52 {
53  unsigned int i, oldSize = size();
54  KMMsgBase* msg;
55 
56  // delete messages that will get lost, if any
57  if (aSize < mHigh)
58  {
59  for (i=aSize; i<mHigh; i++)
60  {
61  msg = at(i);
62  if (msg)
63  {
64  delete msg;
65  mCount--;
66  }
67  mHigh = aSize;
68  }
69  }
70 
71  // do the resizing
72  if (!TQMemArray<KMMsgBase*>::resize(aSize)) return FALSE;
73 
74  // initialize new elements
75  for (i=oldSize; i<aSize; i++)
76  at(i) = 0;
77 
78  return TRUE;
79 }
80 
81 
82 //-----------------------------------------------------------------------------
83 bool KMMsgList::reset(unsigned int aSize)
84 {
85  if (!resize(aSize)) return FALSE;
86  clear();
87  return TRUE;
88 }
89 
90 
91 //-----------------------------------------------------------------------------
92 void KMMsgList::set(unsigned int idx, KMMsgBase* aMsg)
93 {
94  if (idx >= size())
95  resize( idx > 2 * size() ? idx + 16 : 2 * size() );
96 
97  if (!at(idx) && aMsg) mCount++;
98  else if (at(idx) && !aMsg) mCount--;
99 
100  at(idx) = aMsg;
101 
102  if (!aMsg || idx >= mHigh) rethinkHigh();
103 }
104 
105 
106 //-----------------------------------------------------------------------------
107 void KMMsgList::insert(unsigned int idx, KMMsgBase* aMsg, bool syncDict)
108 {
109  if (idx >= size())
110  resize( idx > 2 * size() ? idx + 16 : 2 * size() );
111 
112  if (aMsg) mCount++;
113 
114  for (unsigned int i=mHigh; i>idx; i--) {
115  if ( syncDict )
116  KMMsgDict::mutableInstance()->remove(at(i - 1));
117  at(i) = at(i-1);
118  if ( syncDict )
119  KMMsgDict::mutableInstance()->insert(at(i), i);
120  }
121 
122  at(idx) = aMsg;
123  if ( syncDict )
124  KMMsgDict::mutableInstance()->insert(at(idx), idx);
125 
126  mHigh++;
127 }
128 
129 
130 //-----------------------------------------------------------------------------
131 unsigned int KMMsgList::append(KMMsgBase* aMsg, bool syncDict)
132 {
133  const unsigned int idx = mHigh;
134  insert(idx, aMsg, syncDict); // mHigh gets modified in here
135  return idx;
136 }
137 
138 
139 //-----------------------------------------------------------------------------
140 void KMMsgList::remove(unsigned int idx)
141 {
142  assert(idx<size());
143  if (at(idx)) {
144  mCount--;
145  KMMsgDict::mutableInstance()->remove(at(idx));
146  }
147 
148  mHigh--;
149  for (unsigned int i=idx; i<mHigh; i++) {
150  KMMsgDict::mutableInstance()->update(at(i + 1), i + 1, i);
151  at(i) = at(i+1);
152  }
153 
154  at(mHigh) = 0;
155 
156  rethinkHigh();
157 }
158 
159 
160 //-----------------------------------------------------------------------------
161 KMMsgBase* KMMsgList::take(unsigned int idx)
162 {
163  KMMsgBase* msg=at(idx);
164  remove(idx);
165  return msg;
166 }
167 
168 
169 //-----------------------------------------------------------------------------
171 {
172  unsigned int sz = size();
173 
174  if (mHigh < sz && at(mHigh))
175  {
176  // forward search
177  while (mHigh < sz && at(mHigh))
178  mHigh++;
179  }
180  else
181  {
182  // backward search
183  while (mHigh>0 && !at(mHigh-1))
184  mHigh--;
185  }
186 }
void insert(unsigned int idx, KMMsgBase *msg, bool syncDict=true)
Insert message at given index.
Definition: kmmsglist.cpp:107
KMMsgBase * take(unsigned int idx)
Returns message at given index and removes it from the list.
Definition: kmmsglist.cpp:161
unsigned int append(KMMsgBase *msg, bool syncDict=true)
Append given message after the last used message.
Definition: kmmsglist.cpp:131
~KMMsgList()
Destructor also deletes all messages in the list.
Definition: kmmsglist.cpp:25
bool reset(unsigned int size)
Clear the array and resize it to given size.
Definition: kmmsglist.cpp:83
void remove(unsigned int idx)
Remove message at given index without deleting it.
Definition: kmmsglist.cpp:140
KMMsgList(int initialSize=32)
Constructor with optional initial size.
Definition: kmmsglist.cpp:14
void rethinkHigh()
Set mHigh to proper value.
Definition: kmmsglist.cpp:170
bool resize(unsigned int size)
Resize array and initialize new elements if any.
Definition: kmmsglist.cpp:51
void clear(bool autoDelete=TRUE, bool syncDict=false)
Clear messages.
Definition: kmmsglist.cpp:32
void set(unsigned int idx, KMMsgBase *msg)
Set message at given index.
Definition: kmmsglist.cpp:92