sievedebugdialog.cpp 9.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
    sievedebugdialog.cpp

    Copyright (c) 2005 Martijn Klingens <klingens@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
*/

#include "sievedebugdialog.h"
Laurent Montel's avatar
Laurent Montel committed
15 16
#include <KPIMTextEdit/PlainTextEditorWidget>
#include <KPIMTextEdit/PlainTextEditor>
Laurent Montel's avatar
Laurent Montel committed
17
#include "util/util_p.h"
18 19 20 21

#include "libksieve_debug.h"
#include <KLocalizedString>
#include <kmanagesieve/sievejob.h>
22 23 24 25
#include <KSyntaxHighlighting/SyntaxHighlighter>
#include <KSyntaxHighlighting/Definition>
#include <KSyntaxHighlighting/Theme>

26 27 28 29 30 31 32 33 34
#include <QTimer>
#include <KSharedConfig>
#include <QDialogButtonBox>
#include <KConfigGroup>
#include <QPushButton>
#include <QVBoxLayout>

using namespace KSieveUi;

Laurent Montel's avatar
Laurent Montel committed
35
SieveDebugDialog::SieveDebugDialog(SieveImapPasswordProvider *passwordProvider, QWidget *parent)
Laurent Montel's avatar
Laurent Montel committed
36
    : QDialog(parent)
37
    , mPasswordProvider(passwordProvider)
38
{
39
    setWindowTitle(i18nc("@title:window", "Sieve Diagnostics"));
Laurent Montel's avatar
Laurent Montel committed
40
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
41 42

    // Collect all accounts
Laurent Montel's avatar
Laurent Montel committed
43
    mResourceIdentifier = KSieveUi::Util::sieveImapResourceNames();
44 45 46

    mEdit = new KPIMTextEdit::PlainTextEditorWidget(this);
    mEdit->setReadOnly(true);
Laurent Montel's avatar
Laurent Montel committed
47
    const KSyntaxHighlighting::Definition def = mRepo.definitionForName(QStringLiteral("Sieve"));
48 49 50
    if (!def.isValid()) {
        qCWarning(LIBKSIEVE_LOG) << "Invalid definition name";
    }
51 52 53 54 55 56 57

    KSyntaxHighlighting::SyntaxHighlighter *hl = new KSyntaxHighlighting::SyntaxHighlighter(mEdit->editor()->document());
    hl->setTheme((palette().color(QPalette::Base).lightness() < 128)
                 ? mRepo.defaultTheme(KSyntaxHighlighting::Repository::DarkTheme)
                 : mRepo.defaultTheme(KSyntaxHighlighting::Repository::LightTheme));
    hl->setDefinition(def);

Laurent Montel's avatar
Laurent Montel committed
58 59 60
    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, this);
    connect(buttonBox, &QDialogButtonBox::rejected, this, &SieveDebugDialog::reject);

61 62 63 64 65 66 67
    mainLayout->addWidget(mEdit);
    mainLayout->addWidget(buttonBox);

    if (!mResourceIdentifier.isEmpty()) {
        mEdit->editor()->setPlainText(i18n("Collecting diagnostic information about Sieve support...\n\n"));
        QTimer::singleShot(0, this, &SieveDebugDialog::slotDiagNextAccount);
    } else {
Luigi Toscano's avatar
Luigi Toscano committed
68
        mEdit->editor()->setPlainText(i18n("No IMAP resource found."));
69 70 71 72 73 74 75 76
    }
    readConfig();
}

SieveDebugDialog::~SieveDebugDialog()
{
    if (mSieveJob) {
        mSieveJob->kill();
Laurent Montel's avatar
Laurent Montel committed
77
        mSieveJob = nullptr;
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    }
    qCDebug(LIBKSIEVE_LOG);
    writeConfig();
}

void SieveDebugDialog::readConfig()
{
    KConfigGroup group(KSharedConfig::openConfig(), "SieveDebugDialog");
    const QSize sizeDialog = group.readEntry("Size", QSize(640, 480));
    if (sizeDialog.isValid()) {
        resize(sizeDialog);
    }
}

void SieveDebugDialog::writeConfig()
{
    KConfigGroup group(KSharedConfig::openConfig(), "SieveDebugDialog");
    group.writeEntry("Size", size());
}

void SieveDebugDialog::slotShutDownJob()
{
    disconnect(mSieveJob, &KManageSieve::SieveJob::gotList, this, &SieveDebugDialog::slotGetScriptList);
    mSieveJob->kill();
Laurent Montel's avatar
Laurent Montel committed
102
    mSieveJob = nullptr;
103
    mEdit->editor()->appendPlainText(i18n("Unable to get the info\n\n"));
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    mResourceIdentifier.pop_front();
    QTimer::singleShot(0, this, &SieveDebugDialog::slotDiagNextAccount);
}

void SieveDebugDialog::slotDiagNextAccount()
{
    if (mResourceIdentifier.isEmpty()) {
        return;
    }
    if (!mShutDownJob) {
        mShutDownJob = new QTimer(this);
        mShutDownJob->setSingleShot(true);
        connect(mShutDownJob, &QTimer::timeout, this, &SieveDebugDialog::slotShutDownJob);
    }
    mShutDownJob->start(30 * 1000); // 30 seconds
    QString ident = mResourceIdentifier.first();

    mEdit->editor()->appendPlainText(i18n("Collecting data for account '%1'...\n", ident));
    mEdit->editor()->appendPlainText(i18n("------------------------------------------------------------\n"));

    // Detect URL for this IMAP account
125
    const KSieveUi::Util::AccountInfo info = KSieveUi::Util::fullAccountInfo(ident, mPasswordProvider);
Laurent Montel's avatar
Laurent Montel committed
126
    const QUrl url = info.sieveUrl;
127 128 129 130 131 132 133
    if (!url.isValid()) {
        mEdit->editor()->appendPlainText(i18n("(Account does not support Sieve)\n\n"));
    } else {
        mUrl = url;

        mSieveJob = KManageSieve::SieveJob::list(mUrl);

134 135
        //Laurent: not necessary as it's a readonly dialog
        //mSieveJob->setProperty("sieveimapaccountsettings", QVariant::fromValue(info.sieveImapAccountSettings));
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
        connect(mSieveJob, &KManageSieve::SieveJob::gotList, this, &SieveDebugDialog::slotGetScriptList);

        // Bypass the singleShot timer -- it's fired when we get our data
        return;
    }

    // Handle next account async
    mResourceIdentifier.pop_front();
    QTimer::singleShot(0, this, &SieveDebugDialog::slotDiagNextAccount);
}

void SieveDebugDialog::slotDiagNextScript()
{
    if (mScriptList.isEmpty()) {
        // Continue handling accounts instead
        mScriptList.clear();
        mResourceIdentifier.pop_front();
        QTimer::singleShot(0, this, &SieveDebugDialog::slotDiagNextAccount);
        return;
    }

Laurent Montel's avatar
Laurent Montel committed
157
    QString scriptFile = mScriptList.constFirst();
158 159 160 161
    mScriptList.pop_front();

    mEdit->editor()->appendPlainText(i18n("Contents of script '%1':\n", scriptFile));

Laurent Montel's avatar
Laurent Montel committed
162
    const KSieveUi::Util::AccountInfo info = KSieveUi::Util::fullAccountInfo(mResourceIdentifier.constFirst(), mPasswordProvider);
Laurent Montel's avatar
Laurent Montel committed
163
    mUrl = info.sieveUrl;
164 165

    mUrl = mUrl.adjusted(QUrl::RemoveFilename);
Laurent Montel's avatar
Laurent Montel committed
166
    mUrl.setPath(mUrl.path() + QLatin1Char('/') + scriptFile);
167 168 169 170 171 172

    mSieveJob = KManageSieve::SieveJob::get(mUrl);

    connect(mSieveJob, &KManageSieve::SieveJob::gotScript, this, &SieveDebugDialog::slotGetScript);
}

Laurent Montel's avatar
Laurent Montel committed
173
void SieveDebugDialog::slotGetScript(KManageSieve::SieveJob *job, bool success, const QString &script, bool active)
174 175
{
    qCDebug(LIBKSIEVE_LOG) << "( ??," << success
176 177
                           << ", ?," << active << ")"
                          #if (QT_VERSION < QT_VERSION_CHECK(5, 15, 0))
Laurent Montel's avatar
Laurent Montel committed
178
        << endl
179
                          #else
Laurent Montel's avatar
Laurent Montel committed
180
        << Qt::endl
181
                          #endif
Laurent Montel's avatar
Laurent Montel committed
182
        << "script:"
183
                          #if (QT_VERSION < QT_VERSION_CHECK(5, 15, 0))
Laurent Montel's avatar
Laurent Montel committed
184
        << endl
185
                          #else
Laurent Montel's avatar
Laurent Montel committed
186
        << Qt::endl
187
                          #endif
Laurent Montel's avatar
Laurent Montel committed
188
        << script;
Laurent Montel's avatar
Laurent Montel committed
189
    mSieveJob = nullptr; // job deletes itself after returning from this slot!
190

191 192 193 194
    if (!success) {
        mEdit->editor()->appendPlainText(i18n("Retrieving the script failed.\n"
                                              "The server responded:\n%1", job->errorString()));
    } else if (script.isEmpty()) {
Laurent Montel's avatar
Laurent Montel committed
195
        mEdit->editor()->appendPlainText(i18n("(This script is empty)\n\n"));
196 197 198 199 200 201 202 203 204 205 206
    } else {
        mEdit->editor()->appendPlainText(i18n(
                                             "------------------------------------------------------------\n"
                                             "%1\n"
                                             "------------------------------------------------------------\n\n", script));
    }

    // Fetch next script
    QTimer::singleShot(0, this, &SieveDebugDialog::slotDiagNextScript);
}

Laurent Montel's avatar
Laurent Montel committed
207
void SieveDebugDialog::slotGetScriptList(KManageSieve::SieveJob *job, bool success, const QStringList &scriptList, const QString &activeScript)
208 209 210 211
{
    if (mShutDownJob->isActive()) {
        mShutDownJob->stop();
    }
Laurent Montel's avatar
Laurent Montel committed
212 213
    qCDebug(LIBKSIEVE_LOG) << "Success:" << success << ", List:" << scriptList.join(QLatin1Char(','))
                           <<", active:" << activeScript;
Laurent Montel's avatar
Laurent Montel committed
214
    mSieveJob = nullptr; // job deletes itself after returning from this slot!
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238

    mEdit->editor()->appendPlainText(i18n("Sieve capabilities:\n"));
    const QStringList caps = job->sieveCapabilities();
    if (caps.isEmpty()) {
        mEdit->editor()->appendPlainText(i18n("(No special capabilities available)"));
    } else {
        QStringList::const_iterator end = caps.constEnd();
        for (QStringList::const_iterator it = caps.constBegin(); it != end; ++it) {
            mEdit->editor()->appendPlainText(QLatin1String("* ") + *it + QLatin1Char('\n'));
        }
        mEdit->editor()->appendPlainText(QStringLiteral("\n"));
    }

    mEdit->editor()->appendPlainText(i18n("Available Sieve scripts:\n"));

    if (scriptList.isEmpty()) {
        mEdit->editor()->appendPlainText(i18n("(No Sieve scripts available on this server)\n\n"));
    } else {
        mScriptList = scriptList;
        QStringList::const_iterator end = scriptList.constEnd();
        for (QStringList::const_iterator it = scriptList.constBegin(); it != end; ++it) {
            mEdit->editor()->appendPlainText(QLatin1String("* ") + *it + QLatin1Char('\n'));
        }
        mEdit->editor()->appendPlainText(QStringLiteral("\n"));
Laurent Montel's avatar
Laurent Montel committed
239
        mEdit->editor()->appendPlainText(i18n("Active script: '%1'\n\n", activeScript));
240 241 242 243 244
    }

    // Handle next job: dump scripts for this server
    QTimer::singleShot(0, this, &SieveDebugDialog::slotDiagNextScript);
}