Fix links to the help files (Thanks André Marcelo Alvarenga for spotting).
[kdepim.git] / knode / knarticlecollection.cpp
blob30afd9937fb83bf8b29d68f0d2bad41e9f2f836a
1 /*
2 KNode, the KDE newsreader
3 Copyright (c) 1999-2005 the KNode authors.
4 See file AUTHORS for details
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10 You should have received a copy of the GNU General Public License
11 along with this program; if not, write to the Free Software Foundation,
12 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US
15 #include "knarticlecollection.h"
17 #include "knarticle.h"
19 #include <KDebug>
20 #include <QByteArray>
23 KNArticleVector::KNArticleVector(KNArticleVector *master, SortingType sorting)
24 : m_aster( master ),
25 s_ortType( sorting )
29 KNArticleVector::~KNArticleVector()
33 void KNArticleVector::append( KNArticle::Ptr a )
35 if ( a ) {
36 mList.append( a );
40 void KNArticleVector::remove( int pos )
42 if ( pos >= 0 && pos < mList.size() ) {
43 mList.removeAt( pos );
48 void KNArticleVector::clear()
50 mList.clear();
53 void KNArticleVector::syncWithMaster()
55 if (!m_aster) return;
57 mList = m_aster->mList;
58 sort();
62 void KNArticleVector::sort()
64 bool (*cmp)( KNArticle::Ptr, KNArticle::Ptr ) = 0;
66 switch(s_ortType) {
67 case STid:
68 cmp=compareById;
69 break;
70 case STmsgId:
71 cmp=compareByMsgId;
72 break;
73 default:
74 cmp=0;
75 break;
78 if(cmp) {
79 qSort( mList.begin(), mList.end(), cmp );
84 bool KNArticleVector::compareById( KNArticle::Ptr a1, KNArticle::Ptr a2 )
86 return ( a1->id() < a2->id() );
90 bool KNArticleVector::compareByMsgId( KNArticle::Ptr a1, KNArticle::Ptr a2 )
92 QByteArray mid1 = a1->messageID( true )->as7BitString( false );
93 QByteArray mid2 = a2->messageID( true )->as7BitString( false );
94 return ( mid1 < mid2 );
98 KNArticle::Ptr KNArticleVector::bsearch( int id )
100 return at( indexForId( id ) );
104 KNArticle::Ptr KNArticleVector::bsearch( const QByteArray &id )
106 return at ( indexForMsgId( id ) );
110 int KNArticleVector::indexForId(int id)
112 if(s_ortType!=STid) return -1;
114 int start = 0, mid = 0, currentId = 0;
115 int end = mList.size();
116 bool found=false;
118 while(start!=end && !found) {
119 mid=(start+end)/2;
120 currentId = mList[ mid ]->id();
122 if(currentId==id)
123 found=true;
124 else if(currentId < id)
125 start=mid+1;
126 else
127 end=mid;
130 if(found)
131 return mid;
132 else {
133 return -1;
138 int KNArticleVector::indexForMsgId( const QByteArray &id )
140 if(s_ortType!=STmsgId) return -1;
142 int start = 0, mid = 0;
143 int end = mList.size();
144 QByteArray currentMid;
145 bool found=false;
146 int cnt=0;
148 while(start!=end && !found) {
149 mid=(start+end)/2;
150 currentMid = mList[ mid ]->messageID( true )->as7BitString( false );
152 if(currentMid==id)
153 found=true;
154 else if( currentMid < id )
155 start=mid+1;
156 else
157 end=mid;
159 cnt++;
162 if(found) {
163 return mid;
164 } else {
165 return -1;
171 // -------------------------------------------------------------------------------------------
174 KNArticleCollection::KNArticleCollection( KNCollection::Ptr p )
175 : KNCollection(p), l_astID(0), l_ockedArticles(0), n_otUnloadable(false)
177 a_rticles.setSortMode(KNArticleVector::STid);
178 m_idIndex.setSortMode(KNArticleVector::STmsgId);
179 m_idIndex.setMaster(&a_rticles);
182 KNArticleCollection::~KNArticleCollection()
186 void KNArticleCollection::append( KNArticle::Ptr a )
188 a_rticles.append( a );
189 if( a->id() == -1 ) {
190 a->setId( ++l_astID );
194 void KNArticleCollection::remove( const KNArticle::Ptr &art )
196 a_rticles.remove( a_rticles.indexForId( art->id() ) );
200 void KNArticleCollection::clear()
202 a_rticles.clear();
203 m_idIndex.clear();
204 l_astID=0;
208 void KNArticleCollection::compact()
210 m_idIndex.clear();
214 KNArticle::Ptr KNArticleCollection::byId( int id )
216 return a_rticles.bsearch(id);
220 KNArticle::Ptr KNArticleCollection::byMessageId( const QByteArray &mid )
222 if(m_idIndex.isEmpty()) {
223 m_idIndex.syncWithMaster();
224 kDebug(5003) <<"KNArticleCollection::byMessageId() : created index";
226 return m_idIndex.bsearch(mid);
230 void KNArticleCollection::setLastID()
232 if ( !a_rticles.isEmpty() ) {
233 l_astID = a_rticles.at( a_rticles.size()-1 )->id();
234 } else {
235 l_astID=0;
240 void KNArticleCollection::syncSearchIndex()
242 m_idIndex.syncWithMaster();