~ubuntu-branches/ubuntu/oneiric/kdepim/oneiric-updates

« back to all changes in this revision

Viewing changes to kmail/sievejob.cpp

  • Committer: Package Import Robot
  • Author(s): Philip Muškovac
  • Date: 2011-06-28 19:33:24 UTC
  • mfrom: (0.2.13) (0.1.13 sid)
  • Revision ID: package-import@ubuntu.com-20110628193324-8yvjs8sdv9rdoo6c
Tags: 4:4.7.0-0ubuntu1
* New upstream release
  - update install files
  - add missing kdepim-doc package to control file
  - Fix Vcs lines
  - kontact breaks/replaces korganizer << 4:4.6.80
  - tighten the dependency of kdepim-dev on libkdepim4 to fix lintian error

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  -*- c++ -*-
2
 
    sievejob.h
3
 
 
4
 
    KMail, the KDE mail client.
5
 
    Copyright (c) 2002 Marc Mutz <mutz@kde.org>
6
 
 
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
13
 
*/
14
 
 
15
 
#include "sievejob.h"
16
 
 
17
 
#include <kjobtrackerinterface.h>
18
 
#include <kio/job.h>
19
 
#include <kio/deletejob.h>
20
 
#include <kio/jobuidelegate.h>
21
 
using KIO::Job;
22
 
// <kio/global.h>
23
 
using KIO::UDSEntryList;
24
 
using KIO::UDSEntry;
25
 
#include <kdebug.h>
26
 
 
27
 
#include <QTextCodec>
28
 
 
29
 
#include <cassert>
30
 
 
31
 
namespace KMail {
32
 
 
33
 
  SieveJob::SieveJob( const KUrl & url, const QString & script,
34
 
                      const QStack<Command> & commands,
35
 
                      QObject * parent, const char * name )
36
 
    : QObject( parent ),
37
 
      mUrl( url ), mJob( 0 ), mDec( 0 ),
38
 
      mScript( script ), mFileExists( DontKnow ), mCommands( commands )
39
 
  {
40
 
    setObjectName( name );
41
 
    assert( !commands.isEmpty() );
42
 
    schedule( commands.top() );
43
 
  }
44
 
 
45
 
  SieveJob::~SieveJob() {
46
 
    kill();
47
 
    delete mDec;
48
 
    kDebug() << "~SieveJob()";
49
 
  }
50
 
 
51
 
  void SieveJob::kill( KJob::KillVerbosity verbosity ) {
52
 
    if ( mJob ) mJob->kill( verbosity );
53
 
  }
54
 
 
55
 
  void SieveJob::setInteractive( bool interactive ) {
56
 
    if ( mJob && !interactive ) {
57
 
      mJob->setUiDelegate( 0 );
58
 
      KIO::getJobTracker()->unregisterJob(mJob);
59
 
    }
60
 
  }
61
 
 
62
 
  void SieveJob::schedule( Command command ) {
63
 
    switch ( command ) {
64
 
    case Get:
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&)) );
69
 
      break;
70
 
    case Put:
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&)) );
75
 
      break;
76
 
    case Activate:
77
 
      kDebug() << "chmod(" << mUrl.prettyUrl() <<", 0700 )";
78
 
      mJob = KIO::chmod( mUrl, 0700 );
79
 
      break;
80
 
    case Deactivate:
81
 
      kDebug() << "chmod(" << mUrl.prettyUrl() <<", 0600 )";
82
 
      mJob = KIO::chmod( mUrl, 0600 );
83
 
      break;
84
 
    case SearchActive:
85
 
      kDebug() << "listDir(" << mUrl.prettyUrl() << ")";
86
 
      {
87
 
        KUrl url = mUrl;
88
 
        QString query = url.query(); //save query part, because KUrl::cd() erases it
89
 
        if ( !url.fileName().isEmpty() )
90
 
          url.cd("..");
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&)) );
96
 
        break;
97
 
      }
98
 
    case List:
99
 
      kDebug() << "listDir(" << mUrl.prettyUrl() << ")";
100
 
      {
101
 
        mJob = KIO::listDir( mUrl );
102
 
        connect( mJob, SIGNAL( entries(KIO::Job *, const KIO::UDSEntryList & ) ),
103
 
                 SLOT( slotEntries( KIO::Job *, const KIO::UDSEntryList & ) ) );
104
 
        break;
105
 
      }
106
 
    case Delete:
107
 
      kDebug() << "delete(" << mUrl.prettyUrl() << ")";
108
 
      mJob = KIO::del( mUrl );
109
 
      break;
110
 
    default:
111
 
      assert( 0 );
112
 
    }
113
 
    // common to all jobs:
114
 
    connect( mJob, SIGNAL(result(KJob*)), SLOT(slotResult(KJob*)) );
115
 
  }
116
 
 
117
 
  void SieveJob::slotData( Job *, const QByteArray & data ) {
118
 
    // check for end-of-data marker:
119
 
    if ( data.size() == 0 )
120
 
      return;
121
 
 
122
 
    // make sure we have a textdecoder;
123
 
    if ( !mDec )
124
 
      mDec = QTextCodec::codecForMib( 106 /*utf8*/ )->makeDecoder();
125
 
 
126
 
    // decode utf8; add to mScript:
127
 
    mScript += mDec->toUnicode( data.data(), data.size() );
128
 
  }
129
 
 
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
134
 
      return;
135
 
    }
136
 
 
137
 
    // Convert mScript into UTF-8:
138
 
    data = mScript.toUtf8();
139
 
 
140
 
    // "data" contains a trailing NUL, remove:
141
 
    if ( data.size() > 0 && data[(int)data.size() - 1] == '\0' )
142
 
      data.resize( data.size() - 1 );
143
 
 
144
 
    // mark mScript sent:
145
 
    mScript.clear();
146
 
  }
147
 
 
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 );
157
 
 
158
 
      if ( isActive )
159
 
        mActiveScriptName = filename;
160
 
 
161
 
      if ( mFileExists == DontKnow && filename == mUrl.fileName() )
162
 
        mFileExists = Yes;
163
 
      emit item( this, filename, isActive );
164
 
      if ( mFileExists == Yes && !mActiveScriptName.isEmpty() )
165
 
        return; // early return if we have all information
166
 
    }
167
 
  }
168
 
 
169
 
  void SieveJob::slotResult( KJob * job ) {
170
 
    Command lastCmd = mCommands.top();
171
 
 
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
174
 
    // listDir...
175
 
    if ( lastCmd == SearchActive && mFileExists == DontKnow && !job->error() )
176
 
      mFileExists = No;
177
 
    // prepare for next round:
178
 
    mCommands.pop();
179
 
    delete mDec; mDec = 0;
180
 
 
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");
184
 
    }
185
 
 
186
 
    // check for errors:
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();
191
 
      }
192
 
 
193
 
      emit result( this, false, mScript, mUrl.fileName() == mActiveScriptName );
194
 
 
195
 
      if ( lastCmd == List )
196
 
        emit gotList( this, false, mAvailableScripts, mActiveScriptName );
197
 
      else
198
 
        emit gotScript( this, false, mScript, mUrl.fileName() == mActiveScriptName );
199
 
 
200
 
      mJob = 0;
201
 
      deleteLater();
202
 
      return;
203
 
    }
204
 
 
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 ) {
209
 
        mScript.clear();
210
 
        mCommands.pop();
211
 
      }
212
 
    }
213
 
 
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 );
219
 
      else
220
 
        emit gotScript( this, true, mScript, mUrl.fileName() == mActiveScriptName );
221
 
 
222
 
      mJob = 0; // deletes itself on returning from this slot
223
 
      deleteLater();
224
 
      return;
225
 
    } else {
226
 
      // schedule the next command:
227
 
      schedule( mCommands.top() );
228
 
    }
229
 
  }
230
 
 
231
 
  SieveJob * SieveJob::put( const KUrl & dest, const QString & script,
232
 
                            bool makeActive, bool wasActive ) {
233
 
    QStack<Command> commands;
234
 
    if ( makeActive )
235
 
      commands.push( Activate );
236
 
    if ( wasActive )
237
 
      commands.push( Deactivate );
238
 
    commands.push( Put );
239
 
    return new SieveJob( dest, script, commands );
240
 
  }
241
 
 
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 );
247
 
  }
248
 
 
249
 
  SieveJob * SieveJob::list( const KUrl & src ) {
250
 
    QStack<Command> commands;
251
 
    commands.push( List );
252
 
    return new SieveJob( src, QString(), commands );
253
 
  }
254
 
  SieveJob * SieveJob::del( const KUrl & url ) {
255
 
    QStack<Command> commands;
256
 
    commands.push( Delete );
257
 
    return new SieveJob( url, QString(), commands );
258
 
  }
259
 
 
260
 
  SieveJob * SieveJob::desactivate( const KUrl & url ) {
261
 
    QStack<Command> commands;
262
 
    commands.push( Deactivate );
263
 
    return new SieveJob( url, QString(), commands );
264
 
  }
265
 
 
266
 
  SieveJob * SieveJob::activate( const KUrl & url ) {
267
 
    QStack<Command> commands;
268
 
    commands.push( Activate );
269
 
    return new SieveJob( url, QString(), commands );
270
 
  }
271
 
 
272
 
} // namespace KMail
273
 
 
274
 
#include "sievejob.moc"
275
 
 
276
 
// vim: set noet sts=2 ts=8 sw=2:
277