11#include "sievejob_p.h"
13#include "kmanagersieve_debug.h"
14#include <KLocalizedString>
17using namespace KManageSieve;
21Session *SieveJob::Private::sessionForUrl(
const QUrl &url)
28 m_sessionPool.insert(hostUrl, sessionPtr);
29 sessionPtr->connectToHost(hostUrl);
31 if (sessionPtr->disconnected()) {
32 sessionPtr->connectToHost(hostUrl);
35 return sessionPtr.
data();
43 const unsigned int oldOutSize = out.
size();
45 const char *s = in.
begin();
46 const char *
const end = in.
end();
47 char *d = out.
begin() + oldOutSize;
50 if (*s ==
'\n' && last !=
'\r') {
58void SieveJob::Private::run(
Session *session)
60 switch (mCommands.top()) {
62 const QString filename = mUrl.fileName();
63 session->sendData(
"GETSCRIPT \"" + filename.
toUtf8() +
"\"");
67 const QString filename = mUrl.fileName();
69 append_lf2crlf(encodedData, mScript.toUtf8());
71 session->sendData(encodedData);
75 const QString filename = mUrl.fileName();
76 session->sendData(
"SETACTIVE \"" + filename.
toUtf8() +
"\"");
80 session->sendData(
"SETACTIVE \"\"");
84 session->sendData(
"LISTSCRIPTS");
87 const QString filename = mUrl.fileName();
88 session->sendData(
"DELETESCRIPT \"" + filename.
toUtf8() +
"\"");
92 const QString filename = mUrl.fileName();
94 session->sendData(ba);
99 append_lf2crlf(encodedData, mScript.toUtf8());
101 session->sendData(encodedData);
107bool SieveJob::Private::handleResponse(
const Response &response,
const QByteArray &data)
109 if (mCommands.isEmpty()) {
112 const Command lastCmd = mCommands.top();
116 if (response.type() != Response::Action) {
124 mAvailableScripts.append(filename);
125 const bool isActive = response.extra() ==
"ACTIVE";
128 mActiveScriptName = filename;
131 if (mFileExists == DontKnow && filename == mUrl.fileName()) {
135 Q_EMIT q->item(q, filename, isActive);
139 if (response.type() == Response::KeyValuePair) {
141 mErrorMessage =
i18n(
142 "The script did not upload successfully.\n"
143 "This is probably due to errors in the script.\n"
144 "The server responded:\n%1",
146 }
else if (response.type() == Response::Quantity) {
148 mErrorMessage =
i18n(
149 "The script did not upload successfully.\n"
150 "This is probably due to errors in the script.\n"
151 "The server responded:\n%1",
154 mErrorMessage =
i18n(
"The script did not upload successfully.\nThe script may contain errors.");
158 qCDebug(KMANAGERSIEVE_LOG) <<
"Unhandled response: " << response.key() << response.value() << response.extra() << data;
160 if (lastCmd != Put) {
168 if (lastCmd == SearchActive && mFileExists == DontKnow && response.operationSuccessful()) {
175 if (!response.operationSuccessful()) {
176 if (mErrorMessage.isEmpty()) {
185 Q_EMIT q->result(q,
false, mScript, (mUrl.fileName() == mActiveScriptName));
187 if (lastCmd == List) {
188 Q_EMIT q->gotList(q,
false, mAvailableScripts, mActiveScriptName);
190 Q_EMIT q->gotScript(q,
false, mScript, (mUrl.fileName() == mActiveScriptName));
197 if (!mCommands.empty()) {
199 if ((mCommands.top() == Get) && (mFileExists == No)) {
205 if (mCommands.empty()) {
207 Q_EMIT q->result(q,
true, mScript, (mUrl.fileName() == mActiveScriptName));
208 if (lastCmd == List) {
209 Q_EMIT q->gotList(q,
true, mAvailableScripts, mActiveScriptName);
211 Q_EMIT q->gotScript(q,
true, mScript, (mUrl.fileName() == mActiveScriptName));
218 run(sessionForUrl(mUrl));
223void SieveJob::Private::killed()
225 Q_EMIT q->result(q,
false, mScript, (mUrl.fileName() == mActiveScriptName));
226 if (mCommands.top() == List) {
227 Q_EMIT q->gotList(q,
false, mAvailableScripts, mActiveScriptName);
229 Q_EMIT q->gotScript(q,
false, mScript, (mUrl.fileName() == mActiveScriptName));
233SieveJob::SieveJob(
QObject *parent)
235 , d(new Private(this))
248 if (d->mCommands.isEmpty()) {
251 Private::sessionForUrl(d->mUrl)->killJob(
this, verbosity);
256 Session *session = d->sessionForUrl(d->mUrl);
258 return session->sieveExtensions();
265 return d->mFileExists;
270 return d->mErrorMessage;
273void SieveJob::setErrorMessage(
const QString &str)
275 d->mErrorMessage = str;
282 commands.
push(Private::Activate);
286 commands.
push(Private::Deactivate);
289 commands.
push(Private::Put);
292 job->d->mUrl = destination;
293 job->d->mScript = script;
294 job->d->mCommands = commands;
296 Private::sessionForUrl(destination)->scheduleJob(job);
303 commands.
push(Private::Get);
304 commands.
push(Private::SearchActive);
307 job->d->mUrl = source;
308 job->d->mCommands = commands;
310 Private::sessionForUrl(source)->scheduleJob(job);
317 commands.
push(Private::List);
320 job->d->mUrl = source;
321 job->d->mCommands = commands;
323 Private::sessionForUrl(source)->scheduleJob(job);
330 commands.
push(Private::Delete);
334 job->d->mCommands = commands;
336 Private::sessionForUrl(url)->scheduleJob(job);
343 commands.
push(Private::Deactivate);
346 job->d->mCommands = commands;
348 Private::sessionForUrl(url)->scheduleJob(job);
355 commands.
push(Private::Rename);
359 job->d->mNewName = newName;
360 job->d->mCommands = commands;
362 Private::sessionForUrl(url)->scheduleJob(job);
369 commands.
push(Private::Check);
373 job->d->mScript = script;
374 job->d->mCommands = commands;
376 Private::sessionForUrl(url)->scheduleJob(job);
383 commands.
push(Private::Activate);
387 job->d->mCommands = commands;
389 Private::sessionForUrl(url)->scheduleJob(job);
393#include "moc_sievejob.cpp"
A response from a managesieve server.
A network session with a manage sieve server.
A job to manage sieve scripts.
void kill(KJob::KillVerbosity verbosity=KJob::Quietly)
Kills the sieve job.
static SieveJob * deactivate(const QUrl &url)
Deactivates the script with the given sieve url.
bool fileExists() const
Returns whether the requested sieve script exists on the IMAP server.
static SieveJob * check(const QUrl &url, const QString &script)
Check the script with the given sieve url.
QString errorString() const
A human-readable error message.
QStringList sieveCapabilities() const
Returns the sieve capabilities of the IMAP server.
static SieveJob * list(const QUrl &url)
Lists all available scripts at the given sieve url.
static SieveJob * put(const QUrl &destination, const QString &script, bool makeActive, bool wasActive)
Stores a sieve script on an IMAP server.
static SieveJob * del(const QUrl &url)
Deletes the script with the given sieve url.
static SieveJob * rename(const QUrl &url, const QString &newName)
Rename the script with the given sieve url and new name newName.
static SieveJob * get(const QUrl &source)
Gets a sieve script from an IMAP server.
static SieveJob * activate(const QUrl &url)
Activates the script with the given sieve url.
QString i18n(const char *text, const TYPE &arg...)
const QList< QKeySequence > & end()
bool isEmpty() const const
QByteArray number(double n, char format, int precision)
void resize(qsizetype newSize, char c)
qsizetype size() const const
QString fromUtf8(QByteArrayView str)
QByteArray toUtf8() const const
QFuture< T > run(Function function,...)