kmail Library API Documentation

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"
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   KMMsgDict *dict = 0;
00035   if (syncDict)
00036     dict = kmkernel->msgDict();
00037 
00038   if ( mHigh > 0 )
00039     for (unsigned int i=mHigh; i>0; i--)
00040     {
00041       KMMsgBase * msg = at(i-1);
00042       if (msg) {
00043     if (dict)
00044       dict->remove(msg);
00045     at(i-1) = 0;
00046     if (doDelete) delete msg;
00047       }
00048     }
00049   mHigh  = 0;
00050   mCount = 0;
00051 }
00052 
00053 
00054 //-----------------------------------------------------------------------------
00055 bool KMMsgList::resize(unsigned int aSize)
00056 {
00057   unsigned int i, oldSize = size();
00058   KMMsgBase* msg;
00059 
00060   // delete messages that will get lost, if any
00061   if (aSize < mHigh)
00062   {
00063     for (i=aSize; i<mHigh; i++)
00064     {
00065       msg = at(i);
00066       if (msg)
00067       {
00068     delete msg;
00069     mCount--;
00070       }
00071       mHigh = aSize;
00072     }
00073   }
00074 
00075   // do the resizing
00076   if (!QMemArray<KMMsgBase*>::resize(aSize)) return FALSE;
00077 
00078   // initialize new elements
00079   for (i=oldSize; i<aSize; i++)
00080     at(i) = 0;
00081 
00082   return TRUE;
00083 }
00084 
00085 
00086 //-----------------------------------------------------------------------------
00087 bool KMMsgList::reset(unsigned int aSize)
00088 {
00089   if (!resize(aSize)) return FALSE;
00090   clear();
00091   return TRUE;
00092 }
00093 
00094 
00095 //-----------------------------------------------------------------------------
00096 void KMMsgList::set(unsigned int idx, KMMsgBase* aMsg)
00097 {
00098   if (idx >= size())
00099     resize( idx > 2 * size() ? idx + 16 : 2 * size() );
00100 
00101   if (!at(idx) && aMsg) mCount++;
00102   else if (at(idx) && !aMsg) mCount--;
00103 
00104   delete at(idx);
00105   
00106   at(idx) = aMsg;
00107 
00108   if (!aMsg || idx >= mHigh) rethinkHigh();
00109 }
00110 
00111 
00112 //-----------------------------------------------------------------------------
00113 void KMMsgList::insert(unsigned int idx, KMMsgBase* aMsg, bool syncDict)
00114 {
00115   KMMsgDict *dict = 0;
00116   if (syncDict)
00117     dict = kmkernel->msgDict();
00118 
00119   if (idx >= size())
00120     resize( idx > 2 * size() ? idx + 16 : 2 * size() );
00121 
00122   if (aMsg) mCount++;
00123 
00124   for (unsigned int i=mHigh; i>idx; i--) {
00125     if (dict)
00126       dict->remove(at(i - 1));
00127     at(i) = at(i-1);
00128     if (dict)
00129       dict->insert(at(i), i);
00130   }
00131 
00132   at(idx) = aMsg;
00133   if (dict)
00134     dict->insert(at(idx), idx);
00135 
00136   mHigh++;
00137 }
00138 
00139 
00140 //-----------------------------------------------------------------------------
00141 unsigned int KMMsgList::append(KMMsgBase* aMsg, bool syncDict)
00142 {
00143   const unsigned int idx = mHigh;
00144   insert(idx, aMsg, syncDict); // mHigh gets modified in here
00145   return idx;
00146 }
00147 
00148 
00149 //-----------------------------------------------------------------------------
00150 void KMMsgList::remove(unsigned int idx)
00151 {
00152   assert(idx<size());
00153   KMMsgDict *dict = kmkernel->msgDict();
00154   
00155   if (at(idx)) {
00156     mCount--;
00157     if (dict)
00158       dict->remove(at(idx));
00159   }
00160   
00161   mHigh--;
00162   for (unsigned int i=idx; i<mHigh; i++) {
00163     if (dict)
00164       dict->update(at(i + 1), i + 1, i);
00165     at(i) = at(i+1);
00166   }
00167   
00168   at(mHigh) = 0;
00169 
00170   rethinkHigh();
00171 }
00172 
00173 
00174 //-----------------------------------------------------------------------------
00175 KMMsgBase* KMMsgList::take(unsigned int idx)
00176 {
00177   KMMsgBase* msg=at(idx);
00178   remove(idx);
00179   return msg;
00180 }
00181 
00182 
00183 //-----------------------------------------------------------------------------
00184 void KMMsgList::rethinkHigh()
00185 {
00186   unsigned int sz = size();
00187 
00188   if (mHigh < sz && at(mHigh))
00189   {
00190     // forward search
00191     while (mHigh < sz && at(mHigh))
00192       mHigh++;
00193   }
00194   else
00195   {
00196     // backward search
00197     while (mHigh>0 && !at(mHigh-1))
00198       mHigh--;
00199   }
00200 }
00201 
00202 //-----------------------------------------------------------------------------
00203 void KMMsgList::fillMsgDict(KMMsgDict *dict)
00204 {
00205   for (unsigned int idx = 0; idx < mHigh; idx++)
00206     if (at(idx))
00207       dict->insert(0, at(idx), idx);
00208 }
KDE Logo
This file is part of the documentation for kmail Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:37:33 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003