2 Copyright (c) 2005 by Volker Krause <vkrause@kde.org>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 You should have received a copy of the GNU General Public License
9 along with this program; if not, write to the Free Software Foundation,
10 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US
16 #include "kngroupmanager.h"
17 #include "knserverinfo.h"
22 KNode::GroupListJob::GroupListJob( KNJobConsumer
*c
, KNServerInfo::Ptr a
, KNJobItem::Ptr i
, bool incremental
)
23 : KNJobData( KNJobData::JTFetchGroups
, c
, a
, i
),
24 mIncremental( incremental
)
28 void KNode::GroupListJob::execute()
32 KNGroupListData::Ptr target
= boost::static_pointer_cast
<KNGroupListData
>( data() );
34 KUrl destination
= baseUrl();
36 if ( target
->getDescriptions
)
39 query
<< QString( "since=%1%2%3+000000" )
40 .arg( target
->fetchSince
.year() % 100, 2, 10, QChar( '0' ) )
41 .arg( target
->fetchSince
.month(), 2, 10, QChar( '0' ) )
42 .arg( target
->fetchSince
.day(), 2, 10, QChar( '0' ) );
43 destination
.setQuery( query
.join( "&" ) );
44 KIO::ListJob
* job
= KIO::listDir( destination
, KIO::HideProgressInfo
, true );
45 connect( job
, SIGNAL(entries(KIO::Job
*,KIO::UDSEntryList
)),
46 SLOT(slotEntries(KIO::Job
*,KIO::UDSEntryList
)) );
47 connect( job
, SIGNAL(result(KJob
*)), SLOT(slotResult(KJob
*)) );
51 void KNode::GroupListJob::slotEntries( KIO::Job
* job
, const KIO::UDSEntryList
& list
)
54 KNGroupListData::Ptr target
= boost::static_pointer_cast
<KNGroupListData
>( data() );
58 KNGroup::Status access
;
59 for( KIO::UDSEntryList::ConstIterator it
= list
.begin(); it
!= list
.end(); ++it
) {
60 access
= KNGroup::unknown
;
61 name
= (*it
).stringValue( KIO::UDSEntry::UDS_NAME
);
62 desc
= (*it
).stringValue( KIO::UDSEntry::UDS_EXTRA
);
64 int value
= (*it
).numberValue( KIO::UDSEntry::UDS_ACCESS
, -1 );
67 access
= KNGroup::postingAllowed
;
68 else if ( value
& S_IWGRP
)
69 access
= KNGroup::moderated
;
71 access
= KNGroup::readOnly
;
76 if ( target
->subscribed
.contains( name
) ) {
77 target
->subscribed
.removeAll( name
); // group names are unique, we wont find it again anyway...
82 kDebug() << "Found group " << name
;
84 mGroupList
.append(KNGroupInfo( name
, desc
, true, subscribed
, access
) );
86 target
->groups
->append(KNGroupInfo( name
, desc
, false, subscribed
, access
) );
90 void KNode::GroupListJob::slotResult( KJob
* job
)
93 setError( job
->error(), job
->errorString() );
95 KNGroupListData::Ptr target
= boost::static_pointer_cast
<KNGroupListData
>( data() );
97 // TODO: use thread weaver here?
99 setStatus( i18n("Loading group list from disk...") );
100 if ( !target
->readIn( this ) ) {
101 setError( KIO::ERR_COULD_NOT_READ
, i18n("Unable to read the group list file") );
105 target
->merge( &mGroupList
);
107 setStatus( i18n("Writing group list to disk...") );
109 if ( !target
->writeOut() )
110 setError( KIO::ERR_COULD_NOT_WRITE
, i18n("Unable to write the group list file") );
118 KNode::GroupLoadJob::GroupLoadJob( KNJobConsumer
*c
, KNServerInfo::Ptr a
, KNJobItem::Ptr i
) :
119 KNJobData( KNJobData::JTLoadGroups
, c
, a
, i
)
123 void KNode::GroupLoadJob::execute( )
125 KNGroupListData::Ptr target
= boost::static_pointer_cast
<KNGroupListData
>( data() );
127 setStatus( i18n("Loading group list from disk...") );
128 // TODO: use the thread weaver here
129 if ( !target
->readIn( this ) )
130 setError( KIO::ERR_COULD_NOT_READ
, i18n("Unable to read the group list file") );
137 KNode::ArticleListJob::ArticleListJob( KNJobConsumer
*c
, KNServerInfo::Ptr a
, KNJobItem::Ptr i
, bool silent
) :
138 KNJobData( JTfetchNewHeaders
, c
, a
, i
),
143 void KNode::ArticleListJob::execute()
145 mArticleList
.clear();
147 KNGroup::Ptr target
= boost::static_pointer_cast
<KNGroup
>( data() );
149 KUrl destination
= baseUrl();
150 destination
.setPath( target
->groupname() );
152 query
<< "first=" + QString::number( target
->lastNr() + 1 );
153 if ( target
->lastNr() <= 0 ) // first fetch
154 query
<< "max=" + QString::number( target
->maxFetch() );
155 destination
.setQuery( query
.join( "&" ) );
156 KIO::ListJob
* job
= KIO::listDir( destination
, KIO::HideProgressInfo
, true );
157 connect( job
, SIGNAL(entries(KIO::Job
*,KIO::UDSEntryList
)),
158 SLOT(slotEntries(KIO::Job
*,KIO::UDSEntryList
)) );
159 connect( job
, SIGNAL(result(KJob
*)), SLOT(slotResult(KJob
*)) );
163 void KNode::ArticleListJob::slotEntries( KIO::Job
* job
, const KIO::UDSEntryList
& list
)
166 mArticleList
+= list
;
169 void KNode::ArticleListJob::slotResult( KJob
* _job
)
171 Q_ASSERT( mJob
== _job
);
172 KIO::Job
*job
= static_cast<KIO::Job
*>( _job
);
174 setError( job
->error(), job
->errorString() );
176 createProgressItem();
178 KNGroup::Ptr target
= boost::static_pointer_cast
<KNGroup
>( data() );
179 target
->setLastFetchCount( 0 );
181 setStatus( i18n("Sorting...") );
183 if ( job
->metaData().contains( "FirstSerialNumber" ) ) {
184 int firstSerNum
= job
->metaData()["FirstSerialNumber"].toInt();
185 target
->setFirstNr( firstSerNum
);
188 target
->insortNewHeaders( mArticleList
, this );
190 if ( job
->metaData().contains( "LastSerialNumber" ) ) {
191 int lastSerNum
= job
->metaData()["LastSerialNumber"].toInt();
192 target
->setLastNr( lastSerNum
);
203 KNode::ArticleFetchJob::ArticleFetchJob( KNJobConsumer
*c
, KNServerInfo::Ptr a
, KNJobItem::Ptr i
, bool parse
) :
204 KNJobData( JTfetchArticle
, c
, a
, i
),
205 mParseArticle( parse
)
209 void KNode::ArticleFetchJob::execute()
211 KNRemoteArticle::Ptr target
= boost::static_pointer_cast
<KNRemoteArticle
>( data() );
213 KUrl url
= baseUrl();
215 url
.addPath( boost::static_pointer_cast
<KNGroup
>( target
->collection() )->groupname() );
217 // By default, fetch articles by their server-side Id.
218 // (some server does not understand the "ARTICLE <msg-id>" command correctly (bug #193550))
219 if ( target
->articleNumber() != -1 ) {
220 url
.addPath( QString::number( target
->articleNumber() ) );
222 // User asked to fetch a message by its msg-id
223 url
.addPath( target
->messageID()->as7BitString( false ) );
226 KIO::Job
* job
= KIO::storedGet( url
, KIO::NoReload
, KIO::HideProgressInfo
);
227 connect( job
, SIGNAL(result(KJob
*)), SLOT(slotResult(KJob
*)) );
231 void KNode::ArticleFetchJob::slotResult( KJob
* job
)
234 setError( job
->error(), job
->errorString() );
236 KNRemoteArticle::Ptr target
= boost::static_pointer_cast
<KNRemoteArticle
>( data() );
237 KIO::StoredTransferJob
*j
= static_cast<KIO::StoredTransferJob
*>( job
);
238 QByteArray buffer
= j
->data();
239 buffer
.replace( "\r\n", "\n" ); // TODO: do this in the io-slave?
240 target
->setContent( buffer
);
250 KNode::ArticlePostJob::ArticlePostJob( KNJobConsumer
*c
, KNServerInfo::Ptr a
, KNJobItem::Ptr i
) :
251 KNJobData( JTpostArticle
, c
, a
, i
)
255 void KNode::ArticlePostJob::execute( )
257 KNLocalArticle::Ptr target
= boost::static_pointer_cast
<KNLocalArticle
>( data() );
259 KUrl url
= baseUrl();
261 KIO::Job
* job
= KIO::storedPut( target
->encodedContent( true ), url
, -1, KIO::Overwrite
| KIO::HideProgressInfo
);
262 connect( job
, SIGNAL(result(KJob
*)), SLOT(slotResult(KJob
*)) );
266 void KNode::ArticlePostJob::slotResult( KJob
* job
)
268 if ( job
->error() ) {
269 QString hostname
= job
->errorText();
270 switch ( job
->error() ) {
271 case KIO::ERR_WRITE_ACCESS_DENIED
:
272 setError( job
->error(), i18n( "The server %1 does not allow you to post articles to it.", hostname
) );
274 case KIO::ERR_COULD_NOT_WRITE
:
275 setError( job
->error(), i18n( "The posting of this article to the server %1 failed.\n"
276 "Please check that you are not trying to post to a read-only group.", hostname
) );
279 setError( job
->error(), job
->errorString() );