kmmsglist.cpp
00001 // kmmsglist.cpp 00002 00003 #ifdef HAVE_CONFIG_H 00004 #include <config.h> 00005 #endif 00006 00007 #include "kmmsglist.h" 00008 #include "kmmsgdict.h" // FIXME Till - move those into kmfolderindex 00009 #include "kmkernel.h" 00010 #include <assert.h> 00011 #include <stdlib.h> 00012 00013 //----------------------------------------------------------------------------- 00014 KMMsgList::KMMsgList(int initSize) 00015 : TQMemArray<KMMsgBase*>(initSize), 00016 mHigh( 0 ), mCount( 0 ) 00017 { 00018 if ( size() > 0 ) 00019 for (unsigned int i=size(); i>0; i--) 00020 TQMemArray<KMMsgBase*>::at(i-1) = 0; 00021 } 00022 00023 00024 //----------------------------------------------------------------------------- 00025 KMMsgList::~KMMsgList() 00026 { 00027 clear(TRUE); 00028 } 00029 00030 00031 //----------------------------------------------------------------------------- 00032 void KMMsgList::clear(bool doDelete, bool syncDict) 00033 { 00034 if ( mHigh > 0 ) 00035 for (unsigned int i=mHigh; i>0; i--) 00036 { 00037 KMMsgBase * msg = at(i-1); 00038 if (msg) { 00039 if ( syncDict ) 00040 KMMsgDict::mutableInstance()->remove(msg); 00041 at(i-1) = 0; 00042 if (doDelete) delete msg; 00043 } 00044 } 00045 mHigh = 0; 00046 mCount = 0; 00047 } 00048 00049 00050 //----------------------------------------------------------------------------- 00051 bool KMMsgList::resize(unsigned int aSize) 00052 { 00053 unsigned int i, oldSize = size(); 00054 KMMsgBase* msg; 00055 00056 // delete messages that will get lost, if any 00057 if (aSize < mHigh) 00058 { 00059 for (i=aSize; i<mHigh; i++) 00060 { 00061 msg = at(i); 00062 if (msg) 00063 { 00064 delete msg; 00065 mCount--; 00066 } 00067 mHigh = aSize; 00068 } 00069 } 00070 00071 // do the resizing 00072 if (!TQMemArray<KMMsgBase*>::resize(aSize)) return FALSE; 00073 00074 // initialize new elements 00075 for (i=oldSize; i<aSize; i++) 00076 at(i) = 0; 00077 00078 return TRUE; 00079 } 00080 00081 00082 //----------------------------------------------------------------------------- 00083 bool KMMsgList::reset(unsigned int aSize) 00084 { 00085 if (!resize(aSize)) return FALSE; 00086 clear(); 00087 return TRUE; 00088 } 00089 00090 00091 //----------------------------------------------------------------------------- 00092 void KMMsgList::set(unsigned int idx, KMMsgBase* aMsg) 00093 { 00094 if (idx >= size()) 00095 resize( idx > 2 * size() ? idx + 16 : 2 * size() ); 00096 00097 if (!at(idx) && aMsg) mCount++; 00098 else if (at(idx) && !aMsg) mCount--; 00099 00100 at(idx) = aMsg; 00101 00102 if (!aMsg || idx >= mHigh) rethinkHigh(); 00103 } 00104 00105 00106 //----------------------------------------------------------------------------- 00107 void KMMsgList::insert(unsigned int idx, KMMsgBase* aMsg, bool syncDict) 00108 { 00109 if (idx >= size()) 00110 resize( idx > 2 * size() ? idx + 16 : 2 * size() ); 00111 00112 if (aMsg) mCount++; 00113 00114 for (unsigned int i=mHigh; i>idx; i--) { 00115 if ( syncDict ) 00116 KMMsgDict::mutableInstance()->remove(at(i - 1)); 00117 at(i) = at(i-1); 00118 if ( syncDict ) 00119 KMMsgDict::mutableInstance()->insert(at(i), i); 00120 } 00121 00122 at(idx) = aMsg; 00123 if ( syncDict ) 00124 KMMsgDict::mutableInstance()->insert(at(idx), idx); 00125 00126 mHigh++; 00127 } 00128 00129 00130 //----------------------------------------------------------------------------- 00131 unsigned int KMMsgList::append(KMMsgBase* aMsg, bool syncDict) 00132 { 00133 const unsigned int idx = mHigh; 00134 insert(idx, aMsg, syncDict); // mHigh gets modified in here 00135 return idx; 00136 } 00137 00138 00139 //----------------------------------------------------------------------------- 00140 void KMMsgList::remove(unsigned int idx) 00141 { 00142 assert(idx<size()); 00143 if (at(idx)) { 00144 mCount--; 00145 KMMsgDict::mutableInstance()->remove(at(idx)); 00146 } 00147 00148 mHigh--; 00149 for (unsigned int i=idx; i<mHigh; i++) { 00150 KMMsgDict::mutableInstance()->update(at(i + 1), i + 1, i); 00151 at(i) = at(i+1); 00152 } 00153 00154 at(mHigh) = 0; 00155 00156 rethinkHigh(); 00157 } 00158 00159 00160 //----------------------------------------------------------------------------- 00161 KMMsgBase* KMMsgList::take(unsigned int idx) 00162 { 00163 KMMsgBase* msg=at(idx); 00164 remove(idx); 00165 return msg; 00166 } 00167 00168 00169 //----------------------------------------------------------------------------- 00170 void KMMsgList::rethinkHigh() 00171 { 00172 unsigned int sz = size(); 00173 00174 if (mHigh < sz && at(mHigh)) 00175 { 00176 // forward search 00177 while (mHigh < sz && at(mHigh)) 00178 mHigh++; 00179 } 00180 else 00181 { 00182 // backward search 00183 while (mHigh>0 && !at(mHigh-1)) 00184 mHigh--; 00185 } 00186 }