Libksieve

sievedebugdialog.cpp
1 /*
2  sievedebugdialog.cpp
3 
4  Copyright (c) 2005 Martijn Klingens <[email protected]>
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License,
8  version 2.0, as published by the Free Software Foundation.
9  You should have received a copy of the GNU General Public License
10  along with this program; if not, write to the Free Software Foundation,
11  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US
12 */
13 
14 #include "sievedebugdialog.h"
15 #include <KPIMTextEdit/PlainTextEditorWidget>
16 #include <KPIMTextEdit/PlainTextEditor>
17 #include "util/util_p.h"
18 
19 #include "libksieve_debug.h"
20 #include <KLocalizedString>
21 #include <kmanagesieve/sievejob.h>
22 #include <KSyntaxHighlighting/SyntaxHighlighter>
23 #include <KSyntaxHighlighting/Definition>
24 #include <KSyntaxHighlighting/Theme>
25 
26 #include <QTimer>
27 #include <KSharedConfig>
28 #include <QDialogButtonBox>
29 #include <KConfigGroup>
30 #include <QPushButton>
31 #include <QVBoxLayout>
32 
33 using namespace KSieveUi;
34 
35 SieveDebugDialog::SieveDebugDialog(SieveImapPasswordProvider *passwordProvider, QWidget *parent)
36  : QDialog(parent)
37  , mPasswordProvider(passwordProvider)
38 {
39  setWindowTitle(i18nc("@title:window", "Sieve Diagnostics"));
40  QVBoxLayout *mainLayout = new QVBoxLayout(this);
41 
42  // Collect all accounts
43  mResourceIdentifier = KSieveUi::Util::sieveImapResourceNames();
44 
45  mEdit = new KPIMTextEdit::PlainTextEditorWidget(this);
46  mEdit->setReadOnly(true);
47  const KSyntaxHighlighting::Definition def = mRepo.definitionForName(QStringLiteral("Sieve"));
48  if (!def.isValid()) {
49  qCWarning(LIBKSIEVE_LOG) << "Invalid definition name";
50  }
51 
53  hl->setTheme((palette().color(QPalette::Base).lightness() < 128)
54  ? mRepo.defaultTheme(KSyntaxHighlighting::Repository::DarkTheme)
55  : mRepo.defaultTheme(KSyntaxHighlighting::Repository::LightTheme));
56  hl->setDefinition(def);
57 
59  connect(buttonBox, &QDialogButtonBox::rejected, this, &SieveDebugDialog::reject);
60 
61  mainLayout->addWidget(mEdit);
62  mainLayout->addWidget(buttonBox);
63 
64  if (!mResourceIdentifier.isEmpty()) {
65  mEdit->editor()->setPlainText(i18n("Collecting diagnostic information about Sieve support...\n\n"));
66  QTimer::singleShot(0, this, &SieveDebugDialog::slotDiagNextAccount);
67  } else {
68  mEdit->editor()->setPlainText(i18n("No IMAP resource found."));
69  }
70  readConfig();
71 }
72 
73 SieveDebugDialog::~SieveDebugDialog()
74 {
75  if (mSieveJob) {
76  mSieveJob->kill();
77  mSieveJob = nullptr;
78  }
79  qCDebug(LIBKSIEVE_LOG);
80  writeConfig();
81 }
82 
83 void SieveDebugDialog::readConfig()
84 {
85  KConfigGroup group(KSharedConfig::openConfig(), "SieveDebugDialog");
86  const QSize sizeDialog = group.readEntry("Size", QSize(640, 480));
87  if (sizeDialog.isValid()) {
88  resize(sizeDialog);
89  }
90 }
91 
92 void SieveDebugDialog::writeConfig()
93 {
94  KConfigGroup group(KSharedConfig::openConfig(), "SieveDebugDialog");
95  group.writeEntry("Size", size());
96 }
97 
98 void SieveDebugDialog::slotShutDownJob()
99 {
100  disconnect(mSieveJob, &KManageSieve::SieveJob::gotList, this, &SieveDebugDialog::slotGetScriptList);
101  mSieveJob->kill();
102  mSieveJob = nullptr;
103  mEdit->editor()->appendPlainText(i18n("Unable to get the info\n\n"));
104  mResourceIdentifier.pop_front();
105  QTimer::singleShot(0, this, &SieveDebugDialog::slotDiagNextAccount);
106 }
107 
108 void SieveDebugDialog::slotDiagNextAccount()
109 {
110  if (mResourceIdentifier.isEmpty()) {
111  return;
112  }
113  if (!mShutDownJob) {
114  mShutDownJob = new QTimer(this);
115  mShutDownJob->setSingleShot(true);
116  connect(mShutDownJob, &QTimer::timeout, this, &SieveDebugDialog::slotShutDownJob);
117  }
118  mShutDownJob->start(30 * 1000); // 30 seconds
119  QString ident = mResourceIdentifier.first();
120 
121  mEdit->editor()->appendPlainText(i18n("Collecting data for account '%1'...\n", ident));
122  mEdit->editor()->appendPlainText(i18n("------------------------------------------------------------\n"));
123 
124  // Detect URL for this IMAP account
125  const KSieveUi::Util::AccountInfo info = KSieveUi::Util::fullAccountInfo(ident, mPasswordProvider);
126  const QUrl url = info.sieveUrl;
127  if (!url.isValid()) {
128  mEdit->editor()->appendPlainText(i18n("(Account does not support Sieve)\n\n"));
129  } else {
130  mUrl = url;
131 
132  mSieveJob = KManageSieve::SieveJob::list(mUrl);
133 
134  //Laurent: not necessary as it's a readonly dialog
135  //mSieveJob->setProperty("sieveimapaccountsettings", QVariant::fromValue(info.sieveImapAccountSettings));
136  connect(mSieveJob, &KManageSieve::SieveJob::gotList, this, &SieveDebugDialog::slotGetScriptList);
137 
138  // Bypass the singleShot timer -- it's fired when we get our data
139  return;
140  }
141 
142  // Handle next account async
143  mResourceIdentifier.pop_front();
144  QTimer::singleShot(0, this, &SieveDebugDialog::slotDiagNextAccount);
145 }
146 
147 void SieveDebugDialog::slotDiagNextScript()
148 {
149  if (mScriptList.isEmpty()) {
150  // Continue handling accounts instead
151  mScriptList.clear();
152  mResourceIdentifier.pop_front();
153  QTimer::singleShot(0, this, &SieveDebugDialog::slotDiagNextAccount);
154  return;
155  }
156 
157  QString scriptFile = mScriptList.constFirst();
158  mScriptList.pop_front();
159 
160  mEdit->editor()->appendPlainText(i18n("Contents of script '%1':\n", scriptFile));
161 
162  const KSieveUi::Util::AccountInfo info = KSieveUi::Util::fullAccountInfo(mResourceIdentifier.constFirst(), mPasswordProvider);
163  mUrl = info.sieveUrl;
164 
165  mUrl = mUrl.adjusted(QUrl::RemoveFilename);
166  mUrl.setPath(mUrl.path() + QLatin1Char('/') + scriptFile);
167 
168  mSieveJob = KManageSieve::SieveJob::get(mUrl);
169 
170  connect(mSieveJob, &KManageSieve::SieveJob::gotScript, this, &SieveDebugDialog::slotGetScript);
171 }
172 
173 void SieveDebugDialog::slotGetScript(KManageSieve::SieveJob *job, bool success, const QString &script, bool active)
174 {
175  qCDebug(LIBKSIEVE_LOG) << "( ??," << success
176  << ", ?," << active << ")"
177  #if (QT_VERSION < QT_VERSION_CHECK(5, 15, 0))
178  << endl
179  #else
180  << Qt::endl
181  #endif
182  << "script:"
183  #if (QT_VERSION < QT_VERSION_CHECK(5, 15, 0))
184  << endl
185  #else
186  << Qt::endl
187  #endif
188  << script;
189  mSieveJob = nullptr; // job deletes itself after returning from this slot!
190 
191  if (!success) {
192  mEdit->editor()->appendPlainText(i18n("Retrieving the script failed.\n"
193  "The server responded:\n%1", job->errorString()));
194  } else if (script.isEmpty()) {
195  mEdit->editor()->appendPlainText(i18n("(This script is empty)\n\n"));
196  } else {
197  mEdit->editor()->appendPlainText(i18n(
198  "------------------------------------------------------------\n"
199  "%1\n"
200  "------------------------------------------------------------\n\n", script));
201  }
202 
203  // Fetch next script
204  QTimer::singleShot(0, this, &SieveDebugDialog::slotDiagNextScript);
205 }
206 
207 void SieveDebugDialog::slotGetScriptList(KManageSieve::SieveJob *job, bool success, const QStringList &scriptList, const QString &activeScript)
208 {
209  if (mShutDownJob->isActive()) {
210  mShutDownJob->stop();
211  }
212  qCDebug(LIBKSIEVE_LOG) << "Success:" << success << ", List:" << scriptList.join(QLatin1Char(','))
213  <<", active:" << activeScript;
214  mSieveJob = nullptr; // job deletes itself after returning from this slot!
215 
216  mEdit->editor()->appendPlainText(i18n("Sieve capabilities:\n"));
217  const QStringList caps = job->sieveCapabilities();
218  if (caps.isEmpty()) {
219  mEdit->editor()->appendPlainText(i18n("(No special capabilities available)"));
220  } else {
222  for (QStringList::const_iterator it = caps.constBegin(); it != end; ++it) {
223  mEdit->editor()->appendPlainText(QLatin1String("* ") + *it + QLatin1Char('\n'));
224  }
225  mEdit->editor()->appendPlainText(QStringLiteral("\n"));
226  }
227 
228  mEdit->editor()->appendPlainText(i18n("Available Sieve scripts:\n"));
229 
230  if (scriptList.isEmpty()) {
231  mEdit->editor()->appendPlainText(i18n("(No Sieve scripts available on this server)\n\n"));
232  } else {
233  mScriptList = scriptList;
234  QStringList::const_iterator end = scriptList.constEnd();
235  for (QStringList::const_iterator it = scriptList.constBegin(); it != end; ++it) {
236  mEdit->editor()->appendPlainText(QLatin1String("* ") + *it + QLatin1Char('\n'));
237  }
238  mEdit->editor()->appendPlainText(QStringLiteral("\n"));
239  mEdit->editor()->appendPlainText(i18n("Active script: '%1'\n\n", activeScript));
240  }
241 
242  // Handle next job: dump scripts for this server
243  QTimer::singleShot(0, this, &SieveDebugDialog::slotDiagNextScript);
244 }
RemoveFilename
void setDefinition(const Definition &def) override
bool isValid() const const
QTextStream & endl(QTextStream &stream)
QString join(const QString &separator) const const
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
The SieveImapPasswordProvider class.
void timeout()
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
bool isEmpty() const const
QStringList sieveCapabilities() const
Returns the sieve capabilities of the IMAP server.
Definition: sievejob.cpp:264
void gotScript(KManageSieve::SieveJob *job, bool success, const QString &script, bool active)
This signal is emitted when a get job has finished.
QString errorString() const
A human-readable error message.
Definition: sievejob.cpp:278
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
void gotList(KManageSieve::SieveJob *job, bool success, const QStringList &scriptList, const QString &activeScript)
This signal is emitted when a list job has finished.
QString i18n(const char *text, const TYPE &arg...)
void readConfig()
const QList< QKeySequence > & end()
bool isValid() const const
QList::const_iterator constEnd() const const
static SieveJob * get(const QUrl &source)
Gets a sieve script from an IMAP server.
Definition: sievejob.cpp:310
QList::const_iterator constBegin() const const
virtual void setTheme(const Theme &theme)
static SieveJob * list(const QUrl &url)
Lists all available scripts at the given sieve url.
Definition: sievejob.cpp:324
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
A job to manage sieve scripts.
Definition: sievejob.h:41
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jul 13 2020 23:08:35 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.