1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
/* -*- c++ -*-
sievejob.h
KMail, the KDE mail client.
Copyright (c) 2002 Marc Mutz <mutz@kde.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License,
version 2.0, as published by the Free Software Foundation.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "sievejob.h"
#include <kio/job.h>
using KIO::Job;
// <kio/global.h>
using KIO::UDSAtomTypes;
using KIO::UDSEntryList;
using KIO::UDSEntry;
#include <kdebug.h>
#include <qtextcodec.h>
#include <cassert>
namespace KMail {
SieveJob::SieveJob( const KURL & url, const QString & script,
const QValueStack<Command> & commands,
QObject * parent, const char * name )
: QObject( parent, name ),
mUrl( url ), mJob( 0 ), mDec( 0 ),
mScript( script ), mFileExists( DontKnow ), mCommands( commands ),
mShowProgressInfo(true)
{
assert( !commands.isEmpty() );
schedule( commands.top(), true );
}
SieveJob::SieveJob( const KURL & url, const QString & script,
const QValueStack<Command> & commands,
bool showProgressInfo,
QObject * parent, const char * name )
: QObject( parent, name ),
mUrl( url ), mJob( 0 ), mDec( 0 ),
mScript( script ), mFileExists( DontKnow ), mCommands( commands ),
mShowProgressInfo(showProgressInfo)
{
assert( !commands.isEmpty() );
schedule( commands.top(), showProgressInfo );
}
SieveJob::~SieveJob() {
kill();
delete mDec;
kdDebug(5006) << "~SieveJob()" << endl;
}
void SieveJob::kill( bool quiet ) {
if ( mJob ) mJob->kill( quiet );
}
void SieveJob::schedule( Command command, bool showProgressInfo ) {
switch ( command ) {
case Get:
kdDebug(5006) << "SieveJob::schedule: get( " << mUrl.prettyURL() << " )" << endl;
mJob = KIO::get( mUrl, false /*reload*/, showProgressInfo );
connect( mJob, SIGNAL(data(KIO::Job*,const QByteArray&)),
SLOT(slotData(KIO::Job*,const QByteArray&)) );
break;
case Put:
kdDebug(5006) << "SieveJob::schedule: put( " << mUrl.prettyURL() << " )" << endl;
mJob = KIO::put( mUrl, 0600, true /*overwrite*/, false /*resume*/, showProgressInfo );
connect( mJob, SIGNAL(dataReq(KIO::Job*,QByteArray&)),
SLOT(slotDataReq(KIO::Job*,QByteArray&)) );
break;
case Activate:
kdDebug(5006) << "SieveJob::schedule: chmod( " << mUrl.prettyURL() << ", 0700 )"
<< endl;
mJob = KIO::chmod( mUrl, 0700 );
break;
case Deactivate:
kdDebug(5006) << "SieveJob::schedule: chmod( " << mUrl.prettyURL() << ", 0600 )"
<< endl;
mJob = KIO::chmod( mUrl, 0600 );
break;
case SearchActive:
kdDebug(5006) << "SieveJob::schedule: listDir( " << mUrl.prettyURL() << " )" << endl;
{
KURL url = mUrl;
QString query = url.query(); //save query part, because KURL::cd() erases it
if ( !url.fileName().isEmpty() )
url.cd("..");
url.setQuery( query );
kdDebug(5006) << "SieveJob::schedule: listDir's real URL: " << url.prettyURL()
<< endl;
mJob = KIO::listDir( url, showProgressInfo );
connect( mJob, SIGNAL(entries(KIO::Job*,const KIO::UDSEntryList&)),
SLOT(slotEntries(KIO::Job*,const KIO::UDSEntryList&)) );
break;
}
case List:
kdDebug(5006) << "SieveJob::schedule: listDir( " << mUrl.prettyURL() << " )" << endl;
{
mJob = KIO::listDir( mUrl, showProgressInfo );
connect( mJob, SIGNAL( entries(KIO::Job *, const KIO::UDSEntryList & ) ),
SLOT( slotEntries( KIO::Job *, const KIO::UDSEntryList & ) ) );
break;
}
case Delete:
kdDebug(5006) << "SieveJob::schedule: delete( " << mUrl.prettyURL() << " )" << endl;
mJob = KIO::del( mUrl, false/*shred*/, showProgressInfo );
break;
default:
assert( 0 );
}
mJob->setInteractive(showProgressInfo);
// common to all jobs:
connect( mJob, SIGNAL(result(KIO::Job*)), SLOT(slotResult(KIO::Job*)) );
}
void SieveJob::slotData( Job *, const QByteArray & data ) {
// check for end-of-data marker:
if ( data.size() == 0 )
return;
// make sure we have a textdecoder;
if ( !mDec )
mDec = QTextCodec::codecForMib( 106 /*utf8*/ )->makeDecoder();
// decode utf8; add to mScript:
mScript += mDec->toUnicode( data.data(), data.size() );
}
void SieveJob::slotDataReq( Job *, QByteArray & data ) {
// check whether we have already sent our data:
if ( mScript.isEmpty() ) {
data = QByteArray(); // end-of-data marker
return;
}
// Convert mScript into UTF-8:
data = mScript.utf8();
// "data" contains a trailing NUL, remove:
if ( data.size() > 0 && data[(int)data.size() - 1] == '\0' )
data.resize( data.size() - 1 );
// mark mScript sent:
mScript = QString::null;
}
void SieveJob::slotEntries( Job *, const UDSEntryList & l ) {
// loop over entries:
for ( UDSEntryList::const_iterator it = l.begin() ; it != l.end() ; ++it ) {
// Loop over all UDS atoms to find the UDS_ACCESS and UDS_NAME atoms;
// note if we find an exec'able file ( == active script )
// or the requested filename (mUrl.fileName()).
QString filename;
bool isActive = false;
for ( UDSEntry::const_iterator et = (*it).begin() ; et != (*it).end() ; ++ et ) {
if ( ( *et ).m_uds == KIO::UDS_NAME ) {
filename = ( *et ).m_str;
mAvailableScripts.append( filename );
} else if ( ( *et ).m_uds == KIO::UDS_ACCESS && ( *et ).m_long == 0700 )
isActive = true;
}
if ( isActive )
mActiveScriptName = filename;
if ( mFileExists == DontKnow && filename == mUrl.fileName() )
mFileExists = Yes;
emit item( this, filename, isActive );
if ( mFileExists == Yes && !mActiveScriptName.isEmpty() )
return; // early return if we have all information
}
}
void SieveJob::slotResult( Job * job ) {
Command lastCmd = mCommands.top();
// First, let's see if we come back from a SearchActive. If so, set
// mFileExists to No if we didn't see the mUrl.fileName() during
// listDir...
if ( lastCmd == SearchActive && mFileExists == DontKnow && !job->error() )
mFileExists = No;
// prepare for next round:
mCommands.pop();
delete mDec; mDec = 0;
if ( mSieveCapabilities.empty() ) {
mSieveCapabilities = QStringList::split( ' ', job->queryMetaData( "sieveExtensions" ) );
kdDebug(5006) << "Received Sieve extensions supported:" << endl
<< mSieveCapabilities.join("\n") << endl;
}
// check for errors:
if ( job->error() ) {
if ( job->isInteractive() ) {
job->showErrorDialog( 0 );
}
emit result( this, false, mScript, mUrl.fileName() == mActiveScriptName );
if ( lastCmd == List )
emit gotList( this, false, mAvailableScripts, mActiveScriptName );
else
emit gotScript( this, false, mScript, mUrl.fileName() == mActiveScriptName );
mJob = 0;
delete this;
return;
}
// check for new tasks:
if ( !mCommands.empty() ) {
// Don't fail get'ting a non-existant script:
if ( mCommands.top() == Get && mFileExists == No ) {
mScript = QString::null;
mCommands.pop();
}
}
if ( mCommands.empty() ) {
// was last command; report success and delete this object:
emit result( this, true, mScript, mUrl.fileName() == mActiveScriptName );
if ( lastCmd == List )
emit gotList( this, true, mAvailableScripts, mActiveScriptName );
else
emit gotScript( this, true, mScript, mUrl.fileName() == mActiveScriptName );
mJob = 0; // deletes itself on returning from this slot
delete this;
return;
} else {
// schedule the next command:
schedule( mCommands.top(), mShowProgressInfo );
}
}
SieveJob * SieveJob::put( const KURL & dest, const QString & script,
bool makeActive, bool wasActive ) {
QValueStack<Command> commands;
if ( makeActive )
commands.push( Activate );
if ( wasActive )
commands.push( Deactivate );
commands.push( Put );
return new SieveJob( dest, script, commands );
}
SieveJob * SieveJob::get( const KURL & src, bool showProgressInfo ) {
QValueStack<Command> commands;
commands.push( Get );
commands.push( SearchActive );
return new SieveJob( src, QString::null, commands, showProgressInfo );
}
SieveJob * SieveJob::list( const KURL & src ) {
QValueStack<Command> commands;
commands.push( List );
return new SieveJob( src, QString::null, commands );
}
SieveJob * SieveJob::del( const KURL & url ) {
QValueStack<Command> commands;
commands.push( Delete );
return new SieveJob( url, QString::null, commands );
}
SieveJob * SieveJob::activate( const KURL & url ) {
QValueStack<Command> commands;
commands.push( Activate );
return new SieveJob( url, QString::null, commands );
}
} // namespace KMail
#include "sievejob.moc"
// vim: set noet sts=2 ts=8 sw=2:
|