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"
23 KNArticleVector::KNArticleVector(KNArticleVector
*master
, SortingType sorting
)
29 KNArticleVector::~KNArticleVector()
33 void KNArticleVector::append( KNArticle::Ptr a
)
40 void KNArticleVector::remove( int pos
)
42 if ( pos
>= 0 && pos
< mList
.size() ) {
43 mList
.removeAt( pos
);
48 void KNArticleVector::clear()
53 void KNArticleVector::syncWithMaster()
57 mList
= m_aster
->mList
;
62 void KNArticleVector::sort()
64 bool (*cmp
)( KNArticle::Ptr
, KNArticle::Ptr
) = 0;
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();
118 while(start
!=end
&& !found
) {
120 currentId
= mList
[ mid
]->id();
124 else if(currentId
< id
)
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
;
148 while(start
!=end
&& !found
) {
150 currentMid
= mList
[ mid
]->messageID( true )->as7BitString( false );
154 else if( currentMid
< id
)
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()
208 void KNArticleCollection::compact()
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();
240 void KNArticleCollection::syncSearchIndex()
242 m_idIndex
.syncWithMaster();