21 #include <tdeio/job.h>
24 using TDEIO::UDSAtomTypes;
25 using TDEIO::UDSEntryList;
26 using TDEIO::UDSEntry;
29 #include <tqtextcodec.h>
35 SieveJob::SieveJob(
const KURL & url,
const TQString & script,
36 const TQValueStack<Command> & commands,
37 TQObject * parent,
const char * name )
38 : TQObject( parent, name ),
39 mUrl( url ), mJob( 0 ), mDec( 0 ),
40 mScript( script ), mFileExists( DontKnow ), mCommands( commands ),
41 mShowProgressInfo(true)
43 assert( !commands.isEmpty() );
44 schedule( commands.top(),
true );
47 SieveJob::SieveJob(
const KURL & url,
const TQString & script,
48 const TQValueStack<Command> & commands,
49 bool showProgressInfo,
50 TQObject * parent,
const char * name )
51 : TQObject( parent, name ),
52 mUrl( url ), mJob( 0 ), mDec( 0 ),
53 mScript( script ), mFileExists( DontKnow ), mCommands( commands ),
54 mShowProgressInfo(showProgressInfo)
56 assert( !commands.isEmpty() );
57 schedule( commands.top(), showProgressInfo );
60 SieveJob::~SieveJob() {
63 kdDebug(5006) <<
"~SieveJob()" << endl;
66 void SieveJob::kill(
bool quiet ) {
67 if ( mJob ) mJob->kill( quiet );
70 void SieveJob::schedule( Command command,
bool showProgressInfo ) {
73 kdDebug(5006) <<
"SieveJob::schedule: get( " << mUrl.prettyURL() <<
" )" << endl;
74 mJob = TDEIO::get( mUrl,
false , showProgressInfo );
75 connect( mJob, TQ_SIGNAL(data(TDEIO::Job*,
const TQByteArray&)),
76 TQ_SLOT(slotData(TDEIO::Job*,
const TQByteArray&)) );
79 kdDebug(5006) <<
"SieveJob::schedule: put( " << mUrl.prettyURL() <<
" )" << endl;
80 mJob = TDEIO::put( mUrl, 0600,
true ,
false , showProgressInfo );
81 connect( mJob, TQ_SIGNAL(dataReq(TDEIO::Job*,TQByteArray&)),
82 TQ_SLOT(slotDataReq(TDEIO::Job*,TQByteArray&)) );
85 kdDebug(5006) <<
"SieveJob::schedule: chmod( " << mUrl.prettyURL() <<
", 0700 )"
87 mJob = TDEIO::chmod( mUrl, 0700 );
90 kdDebug(5006) <<
"SieveJob::schedule: chmod( " << mUrl.prettyURL() <<
", 0600 )"
92 mJob = TDEIO::chmod( mUrl, 0600 );
95 kdDebug(5006) <<
"SieveJob::schedule: listDir( " << mUrl.prettyURL() <<
" )" << endl;
98 TQString query = url.query();
99 if ( !url.fileName().isEmpty() )
101 url.setQuery( query );
102 kdDebug(5006) <<
"SieveJob::schedule: listDir's real URL: " << url.prettyURL()
104 mJob = TDEIO::listDir( url, showProgressInfo );
105 connect( mJob, TQ_SIGNAL(entries(TDEIO::Job*,
const TDEIO::UDSEntryList&)),
106 TQ_SLOT(slotEntries(TDEIO::Job*,
const TDEIO::UDSEntryList&)) );
110 kdDebug(5006) <<
"SieveJob::schedule: listDir( " << mUrl.prettyURL() <<
" )" << endl;
112 mJob = TDEIO::listDir( mUrl, showProgressInfo );
113 connect( mJob, TQ_SIGNAL( entries(TDEIO::Job *,
const TDEIO::UDSEntryList & ) ),
114 TQ_SLOT( slotEntries( TDEIO::Job *,
const TDEIO::UDSEntryList & ) ) );
118 kdDebug(5006) <<
"SieveJob::schedule: delete( " << mUrl.prettyURL() <<
" )" << endl;
119 mJob = TDEIO::del( mUrl,
false, showProgressInfo );
124 mJob->setInteractive(showProgressInfo);
126 connect( mJob, TQ_SIGNAL(result(TDEIO::Job*)), TQ_SLOT(slotResult(TDEIO::Job*)) );
129 void SieveJob::slotData( Job *,
const TQByteArray & data ) {
131 if ( data.size() == 0 )
136 mDec = TQTextCodec::codecForMib( 106 )->makeDecoder();
139 mScript += mDec->toUnicode( data.data(), data.size() );
142 void SieveJob::slotDataReq( Job *, TQByteArray & data ) {
144 if ( mScript.isEmpty() ) {
145 data = TQByteArray();
150 data = mScript.utf8();
153 if ( data.size() > 0 && data[(
int)data.size() - 1] ==
'\0' )
154 data.resize( data.size() - 1 );
157 mScript = TQString();
160 void SieveJob::slotEntries( Job *,
const UDSEntryList & l ) {
162 for ( UDSEntryList::const_iterator it = l.begin() ; it != l.end() ; ++it ) {
167 bool isActive =
false;
168 for ( UDSEntry::const_iterator et = (*it).begin() ; et != (*it).end() ; ++ et ) {
169 if ( ( *et ).m_uds == TDEIO::UDS_NAME ) {
170 filename = ( *et ).m_str;
171 mAvailableScripts.append( filename );
172 }
else if ( ( *et ).m_uds == TDEIO::UDS_ACCESS && ( *et ).m_long == 0700 )
177 mActiveScriptName = filename;
179 if ( mFileExists == DontKnow && filename == mUrl.fileName() )
181 emit item(
this, filename, isActive );
182 if ( mFileExists == Yes && !mActiveScriptName.isEmpty() )
187 void SieveJob::slotResult( Job * job ) {
188 Command lastCmd = mCommands.top();
193 if ( lastCmd == SearchActive && mFileExists == DontKnow && !job->error() )
197 delete mDec; mDec = 0;
199 if ( mSieveCapabilities.empty() ) {
200 mSieveCapabilities = TQStringList::split(
' ', job->queryMetaData(
"sieveExtensions" ) );
201 kdDebug(5006) <<
"Received Sieve extensions supported:" << endl
202 << mSieveCapabilities.join(
"\n") << endl;
206 if ( job->error() ) {
207 if ( job->isInteractive() ) {
208 job->showErrorDialog( 0 );
211 emit result(
this,
false, mScript, mUrl.fileName() == mActiveScriptName );
213 if ( lastCmd == List )
214 emit gotList(
this,
false, mAvailableScripts, mActiveScriptName );
216 emit gotScript(
this,
false, mScript, mUrl.fileName() == mActiveScriptName );
224 if ( !mCommands.empty() ) {
226 if ( mCommands.top() == Get && mFileExists == No ) {
227 mScript = TQString();
232 if ( mCommands.empty() ) {
234 emit result(
this,
true, mScript, mUrl.fileName() == mActiveScriptName );
235 if ( lastCmd == List )
236 emit gotList(
this,
true, mAvailableScripts, mActiveScriptName );
238 emit gotScript(
this,
true, mScript, mUrl.fileName() == mActiveScriptName );
245 schedule( mCommands.top(), mShowProgressInfo );
249 SieveJob * SieveJob::put(
const KURL & dest,
const TQString & script,
250 bool makeActive,
bool wasActive ) {
251 TQValueStack<Command> commands;
253 commands.push( Activate );
255 commands.push( Deactivate );
256 commands.push( Put );
257 return new SieveJob( dest, script, commands );
260 SieveJob * SieveJob::get(
const KURL & src,
bool showProgressInfo ) {
261 TQValueStack<Command> commands;
262 commands.push( Get );
263 commands.push( SearchActive );
264 return new SieveJob( src, TQString(), commands, showProgressInfo );
267 SieveJob * SieveJob::list(
const KURL & src ) {
268 TQValueStack<Command> commands;
269 commands.push( List );
270 return new SieveJob( src, TQString(), commands );
272 SieveJob * SieveJob::del(
const KURL & url ) {
273 TQValueStack<Command> commands;
274 commands.push( Delete );
275 return new SieveJob( url, TQString(), commands );
278 SieveJob * SieveJob::desactivate(
const KURL & url ) {
279 TQValueStack<Command> commands;
280 commands.push( Deactivate );
281 return new SieveJob( url, TQString(), commands );
284 SieveJob * SieveJob::activate(
const KURL & url ) {
285 TQValueStack<Command> commands;
286 commands.push( Activate );
287 return new SieveJob( url, TQString(), commands );
292 #include "sievejob.moc"