SVN_SILENT made messages (.desktop file)
[kdepim.git] / knode / nntpjobs.cpp
bloba42b4b4784ea2b981592831f9d9f9929ce88b470
1 /*
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
13 #include "nntpjobs.h"
15 #include "kngroup.h"
16 #include "kngroupmanager.h"
17 #include "knserverinfo.h"
18 #include <kdebug.h>
19 #include <klocale.h>
20 #include <QDir>
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()
30 mGroupList.clear();
32 KNGroupListData::Ptr target = boost::static_pointer_cast<KNGroupListData>( data() );
34 KUrl destination = baseUrl();
35 QStringList query;
36 if ( target->getDescriptions )
37 query << "desc=true";
38 if ( mIncremental )
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*)) );
48 setupKIOJob( job );
51 void KNode::GroupListJob::slotEntries( KIO::Job * job, const KIO::UDSEntryList & list )
53 Q_UNUSED( job );
54 KNGroupListData::Ptr target = boost::static_pointer_cast<KNGroupListData>( data() );
56 QString name, desc;
57 bool subscribed;
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 );
65 if ( value != -1 ) {
66 if( value & S_IWOTH )
67 access = KNGroup::postingAllowed;
68 else if ( value & S_IWGRP )
69 access = KNGroup::moderated;
70 else
71 access = KNGroup::readOnly;
74 if ( name.isEmpty() )
75 continue;
76 if ( target->subscribed.contains( name ) ) {
77 target->subscribed.removeAll( name ); // group names are unique, we wont find it again anyway...
78 subscribed = true;
79 } else {
80 subscribed = false;
82 kDebug() << "Found group " << name;
83 if ( mIncremental )
84 mGroupList.append(KNGroupInfo( name, desc, true, subscribed, access ) );
85 else
86 target->groups->append(KNGroupInfo( name, desc, false, subscribed, access ) );
90 void KNode::GroupListJob::slotResult( KJob * job )
92 if ( job->error() )
93 setError( job->error(), job->errorString() );
94 else {
95 KNGroupListData::Ptr target = boost::static_pointer_cast<KNGroupListData>( data() );
97 // TODO: use thread weaver here?
98 if ( mIncremental ) {
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") );
102 emitFinished();
103 return;
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") );
113 emitFinished();
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") );
132 emitFinished();
137 KNode::ArticleListJob::ArticleListJob( KNJobConsumer *c, KNServerInfo::Ptr a, KNJobItem::Ptr i, bool silent ) :
138 KNJobData( JTfetchNewHeaders, c, a, i ),
139 mSilent( silent )
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() );
151 QStringList query;
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*)) );
160 setupKIOJob( job );
163 void KNode::ArticleListJob::slotEntries( KIO::Job * job, const KIO::UDSEntryList & list )
165 Q_UNUSED( job );
166 mArticleList += list;
169 void KNode::ArticleListJob::slotResult( KJob * _job )
171 Q_ASSERT( mJob == _job );
172 KIO::Job *job = static_cast<KIO::Job*>( _job );
173 if ( job->error() )
174 setError( job->error(), job->errorString() );
175 else {
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 );
196 setComplete();
198 emitFinished();
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() ) );
221 } else {
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*)) );
228 setupKIOJob( job );
231 void KNode::ArticleFetchJob::slotResult( KJob * job )
233 if ( job->error() )
234 setError( job->error(), job->errorString() );
235 else {
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 );
241 if ( mParseArticle )
242 target->parse();
245 emitFinished();
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*)) );
263 setupKIOJob( job );
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 ) );
273 break;
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 ) );
277 break;
278 default:
279 setError( job->error(), job->errorString() );
283 emitFinished();