kmail

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   : QMemArray<KMMsgBase*>(initSize),
00016     mHigh( 0 ), mCount( 0 )
00017 {
00018   if ( size() > 0 )
00019     for (unsigned int i=size(); i>0; i--)
00020     QMemArray<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 (!QMemArray<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   delete at(idx);
00101 
00102   at(idx) = aMsg;
00103 
00104   if (!aMsg || idx >= mHigh) rethinkHigh();
00105 }
00106 
00107 
00108 //-----------------------------------------------------------------------------
00109 void KMMsgList::insert(unsigned int idx, KMMsgBase* aMsg, bool syncDict)
00110 {
00111   if (idx >= size())
00112     resize( idx > 2 * size() ? idx + 16 : 2 * size() );
00113 
00114   if (aMsg) mCount++;
00115 
00116   for (unsigned int i=mHigh; i>idx; i--) {
00117     if ( syncDict )
00118       KMMsgDict::mutableInstance()->remove(at(i - 1));
00119     at(i) = at(i-1);
00120     if ( syncDict )
00121       KMMsgDict::mutableInstance()->insert(at(i), i);
00122   }
00123 
00124   at(idx) = aMsg;
00125   if ( syncDict )
00126     KMMsgDict::mutableInstance()->insert(at(idx), idx);
00127 
00128   mHigh++;
00129 }
00130 
00131 
00132 //-----------------------------------------------------------------------------
00133 unsigned int KMMsgList::append(KMMsgBase* aMsg, bool syncDict)
00134 {
00135   const unsigned int idx = mHigh;
00136   insert(idx, aMsg, syncDict); // mHigh gets modified in here
00137   return idx;
00138 }
00139 
00140 
00141 //-----------------------------------------------------------------------------
00142 void KMMsgList::remove(unsigned int idx)
00143 {
00144   assert(idx<size());
00145   if (at(idx)) {
00146     mCount--;
00147     KMMsgDict::mutableInstance()->remove(at(idx));
00148   }
00149 
00150   mHigh--;
00151   for (unsigned int i=idx; i<mHigh; i++) {
00152     KMMsgDict::mutableInstance()->update(at(i + 1), i + 1, i);
00153     at(i) = at(i+1);
00154   }
00155 
00156   at(mHigh) = 0;
00157 
00158   rethinkHigh();
00159 }
00160 
00161 
00162 //-----------------------------------------------------------------------------
00163 KMMsgBase* KMMsgList::take(unsigned int idx)
00164 {
00165   KMMsgBase* msg=at(idx);
00166   remove(idx);
00167   return msg;
00168 }
00169 
00170 
00171 //-----------------------------------------------------------------------------
00172 void KMMsgList::rethinkHigh()
00173 {
00174   unsigned int sz = size();
00175 
00176   if (mHigh < sz && at(mHigh))
00177   {
00178     // forward search
00179     while (mHigh < sz && at(mHigh))
00180       mHigh++;
00181   }
00182   else
00183   {
00184     // backward search
00185     while (mHigh>0 && !at(mHigh-1))
00186       mHigh--;
00187   }
00188 }
KDE Home | KDE Accessibility Home | Description of Access Keys