4
KMail, the KDE mail client.
5
Copyright (c) 2002 Marc Mutz <mutz@kde.org>
7
This program is free software; you can redistribute it and/or
8
modify it under the terms of the GNU General Public License,
9
version 2.0, as published by the Free Software Foundation.
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
17
#include <kjobtrackerinterface.h>
19
#include <kio/deletejob.h>
20
#include <kio/jobuidelegate.h>
23
using KIO::UDSEntryList;
33
SieveJob::SieveJob( const KUrl & url, const QString & script,
34
const QStack<Command> & commands,
35
QObject * parent, const char * name )
37
mUrl( url ), mJob( 0 ), mDec( 0 ),
38
mScript( script ), mFileExists( DontKnow ), mCommands( commands )
40
setObjectName( name );
41
assert( !commands.isEmpty() );
42
schedule( commands.top() );
45
SieveJob::~SieveJob() {
48
kDebug() << "~SieveJob()";
51
void SieveJob::kill( KJob::KillVerbosity verbosity ) {
52
if ( mJob ) mJob->kill( verbosity );
55
void SieveJob::setInteractive( bool interactive ) {
56
if ( mJob && !interactive ) {
57
mJob->setUiDelegate( 0 );
58
KIO::getJobTracker()->unregisterJob(mJob);
62
void SieveJob::schedule( Command command ) {
65
kDebug() << "get(" << mUrl.prettyUrl() << ")";
66
mJob = KIO::get( mUrl );
67
connect( mJob, SIGNAL(data(KIO::Job*,const QByteArray&)),
68
SLOT(slotData(KIO::Job*,const QByteArray&)) );
71
kDebug() << "put(" << mUrl.prettyUrl() << ")";
72
mJob = KIO::put( mUrl, 0600, KIO::Overwrite );
73
connect( mJob, SIGNAL(dataReq(KIO::Job*,QByteArray&)),
74
SLOT(slotDataReq(KIO::Job*,QByteArray&)) );
77
kDebug() << "chmod(" << mUrl.prettyUrl() <<", 0700 )";
78
mJob = KIO::chmod( mUrl, 0700 );
81
kDebug() << "chmod(" << mUrl.prettyUrl() <<", 0600 )";
82
mJob = KIO::chmod( mUrl, 0600 );
85
kDebug() << "listDir(" << mUrl.prettyUrl() << ")";
88
QString query = url.query(); //save query part, because KUrl::cd() erases it
89
if ( !url.fileName().isEmpty() )
91
url.setQuery( query );
92
kDebug() << "listDir's real URL:" << url.prettyUrl();
93
mJob = KIO::listDir( url );
94
connect( mJob, SIGNAL(entries(KIO::Job*,const KIO::UDSEntryList&)),
95
SLOT(slotEntries(KIO::Job*,const KIO::UDSEntryList&)) );
99
kDebug() << "listDir(" << mUrl.prettyUrl() << ")";
101
mJob = KIO::listDir( mUrl );
102
connect( mJob, SIGNAL( entries(KIO::Job *, const KIO::UDSEntryList & ) ),
103
SLOT( slotEntries( KIO::Job *, const KIO::UDSEntryList & ) ) );
107
kDebug() << "delete(" << mUrl.prettyUrl() << ")";
108
mJob = KIO::del( mUrl );
113
// common to all jobs:
114
connect( mJob, SIGNAL(result(KJob*)), SLOT(slotResult(KJob*)) );
117
void SieveJob::slotData( Job *, const QByteArray & data ) {
118
// check for end-of-data marker:
119
if ( data.size() == 0 )
122
// make sure we have a textdecoder;
124
mDec = QTextCodec::codecForMib( 106 /*utf8*/ )->makeDecoder();
126
// decode utf8; add to mScript:
127
mScript += mDec->toUnicode( data.data(), data.size() );
130
void SieveJob::slotDataReq( Job *, QByteArray & data ) {
131
// check whether we have already sent our data:
132
if ( mScript.isEmpty() ) {
133
data = QByteArray(); // end-of-data marker
137
// Convert mScript into UTF-8:
138
data = mScript.toUtf8();
140
// "data" contains a trailing NUL, remove:
141
if ( data.size() > 0 && data[(int)data.size() - 1] == '\0' )
142
data.resize( data.size() - 1 );
144
// mark mScript sent:
148
void SieveJob::slotEntries( Job *, const UDSEntryList & l ) {
149
// loop over entries:
150
for ( UDSEntryList::const_iterator it = l.begin() ; it != l.end() ; ++it ) {
151
// Loop over all UDS atoms to find the UDSEntry::UDS_ACCESS and UDS_NAME atoms;
152
// note if we find an exec'able file ( == active script )
153
// or the requested filename (mUrl.fileName()).
154
const QString filename = it->stringValue( KIO::UDSEntry::UDS_NAME );
155
mAvailableScripts.append( filename );
156
bool isActive = ( it->numberValue( KIO::UDSEntry::UDS_ACCESS ) == 0700 );
159
mActiveScriptName = filename;
161
if ( mFileExists == DontKnow && filename == mUrl.fileName() )
163
emit item( this, filename, isActive );
164
if ( mFileExists == Yes && !mActiveScriptName.isEmpty() )
165
return; // early return if we have all information
169
void SieveJob::slotResult( KJob * job ) {
170
Command lastCmd = mCommands.top();
172
// First, let's see if we come back from a SearchActive. If so, set
173
// mFileExists to No if we didn't see the mUrl.fileName() during
175
if ( lastCmd == SearchActive && mFileExists == DontKnow && !job->error() )
177
// prepare for next round:
179
delete mDec; mDec = 0;
181
if ( mSieveCapabilities.empty() ) {
182
mSieveCapabilities = static_cast<KIO::Job*>(job)->queryMetaData( "sieveExtensions" ).split(' ', QString::SkipEmptyParts );
183
kDebug() << "Received Sieve extensions supported:\n" << mSieveCapabilities.join("\n");
187
if ( job->error() ) {
188
if ( static_cast<KIO::Job*>(job)->ui() ) {
189
static_cast<KIO::Job*>(job)->ui()->setWindow( 0 );
190
static_cast<KIO::Job*>(job)->ui()->showErrorMessage();
193
emit result( this, false, mScript, mUrl.fileName() == mActiveScriptName );
195
if ( lastCmd == List )
196
emit gotList( this, false, mAvailableScripts, mActiveScriptName );
198
emit gotScript( this, false, mScript, mUrl.fileName() == mActiveScriptName );
205
// check for new tasks:
206
if ( !mCommands.empty() ) {
207
// Don't fail getting a non-existent script:
208
if ( mCommands.top() == Get && mFileExists == No ) {
214
if ( mCommands.empty() ) {
215
// was last command; report success and delete this object:
216
emit result( this, true, mScript, mUrl.fileName() == mActiveScriptName );
217
if ( lastCmd == List )
218
emit gotList( this, true, mAvailableScripts, mActiveScriptName );
220
emit gotScript( this, true, mScript, mUrl.fileName() == mActiveScriptName );
222
mJob = 0; // deletes itself on returning from this slot
226
// schedule the next command:
227
schedule( mCommands.top() );
231
SieveJob * SieveJob::put( const KUrl & dest, const QString & script,
232
bool makeActive, bool wasActive ) {
233
QStack<Command> commands;
235
commands.push( Activate );
237
commands.push( Deactivate );
238
commands.push( Put );
239
return new SieveJob( dest, script, commands );
242
SieveJob * SieveJob::get( const KUrl & src ) {
243
QStack<Command> commands;
244
commands.push( Get );
245
commands.push( SearchActive );
246
return new SieveJob( src, QString(), commands );
249
SieveJob * SieveJob::list( const KUrl & src ) {
250
QStack<Command> commands;
251
commands.push( List );
252
return new SieveJob( src, QString(), commands );
254
SieveJob * SieveJob::del( const KUrl & url ) {
255
QStack<Command> commands;
256
commands.push( Delete );
257
return new SieveJob( url, QString(), commands );
260
SieveJob * SieveJob::desactivate( const KUrl & url ) {
261
QStack<Command> commands;
262
commands.push( Deactivate );
263
return new SieveJob( url, QString(), commands );
266
SieveJob * SieveJob::activate( const KUrl & url ) {
267
QStack<Command> commands;
268
commands.push( Activate );
269
return new SieveJob( url, QString(), commands );
274
#include "sievejob.moc"
276
// vim: set noet sts=2 ts=8 sw=2: