Libksieve

custommanagesievewidget.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
6 
7 #include "custommanagesievewidget.h"
8 #include "util/findaccountinfojob.h"
9 #include "widgets/managesievetreeview.h"
10 
11 #include <KLocalizedString>
12 #include <QIcon>
13 #include <QTreeWidgetItem>
14 #include <kmanagesieve/sievejob.h>
15 #include <widgets/sievetreewidgetitem.h>
16 
17 using namespace KSieveUi;
18 
19 CustomManageSieveWidget::CustomManageSieveWidget(SieveImapPasswordProvider *passwordProvider, QWidget *parent)
20  : KSieveUi::ManageSieveWidget(parent)
21  , mPasswordProvider(passwordProvider)
22 {
23 }
24 
25 CustomManageSieveWidget::~CustomManageSieveWidget()
26 {
27 }
28 
29 void CustomManageSieveWidget::updateSieveSettings()
30 {
31  mSieveImapInstances = KSieveUi::Util::sieveImapInstances();
32  Q_EMIT updateSieveSettingsDone();
33 }
34 
35 bool CustomManageSieveWidget::refreshList()
36 {
37  if (mRefreshInProgress) {
38  return false;
39  }
40  mRefreshInProgress = true;
41  bool noImapFound = true;
42  mLastSieveTreeWidgetItem = nullptr;
43  mServerSieveInfos.clear();
44  for (const KSieveUi::SieveImapInstance &type : mSieveImapInstances) {
45  if (type.status() == KSieveUi::SieveImapInstance::Broken) {
46  continue;
47  }
48  mServerSieveInfos.insert(type.name(), type.identifier());
49  }
50  noImapFound = mServerSieveInfos.isEmpty();
51  searchSieveScript();
52  return noImapFound;
53 }
54 
55 void CustomManageSieveWidget::searchSieveScript()
56 {
57  mSieveServerMapIterator = mServerSieveInfos.constBegin();
58  if (mSieveServerMapIterator != mServerSieveInfos.constEnd()) {
59  slotSearchSieveScript(mSieveServerMapIterator.key(), mSieveServerMapIterator.value());
60  }
61 }
62 
63 void CustomManageSieveWidget::searchNextServerSieve()
64 {
65  ++mSieveServerMapIterator;
66  if (mSieveServerMapIterator != mServerSieveInfos.constEnd()) {
67  slotSearchSieveScript(mSieveServerMapIterator.key(), mSieveServerMapIterator.value());
68  } else {
69  mLastSieveTreeWidgetItem = nullptr;
70  mRefreshInProgress = false;
71  }
72 }
73 
74 void CustomManageSieveWidget::slotSearchSieveScript(const QString &name, const QString &identifier)
75 {
76  mLastSieveTreeWidgetItem = new SieveTreeWidgetItem(treeView(), mLastSieveTreeWidgetItem);
77  mLastSieveTreeWidgetItem->setIcon(0, QIcon::fromTheme(QStringLiteral("network-server")));
78 
79  auto job = new FindAccountInfoJob(this);
80  connect(job, &FindAccountInfoJob::findAccountInfoFinished, this, &CustomManageSieveWidget::slotFindAccountInfoFinished);
81  job->setIdentifier(identifier);
82  job->setProperty("serverName", name);
83  job->setProvider(mPasswordProvider);
84  job->start();
85 }
86 
87 void CustomManageSieveWidget::slotFindAccountInfoFinished(const KSieveUi::Util::AccountInfo &info)
88 {
89  QString serverName = sender()->property("serverName").toString();
90  const QUrl u = info.sieveUrl;
91  if (u.isEmpty()) {
92  auto item = new QTreeWidgetItem(mLastSieveTreeWidgetItem);
93  item->setText(0, i18n("No Sieve URL configured"));
94  item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
95  treeView()->expandItem(mLastSieveTreeWidgetItem);
96  } else {
97  serverName += QStringLiteral(" (%1)").arg(u.userName());
99  // qDebug() << " SETTINGS " << info;
100  job->setProperty("sieveimapaccountsettings", QVariant::fromValue(info.sieveImapAccountSettings));
101  connect(job, &KManageSieve::SieveJob::gotList, this, &CustomManageSieveWidget::slotGotList);
102  mJobs.insert(job, mLastSieveTreeWidgetItem);
103  mUrls.insert(mLastSieveTreeWidgetItem, u);
104  mLastSieveTreeWidgetItem->startAnimation();
105  }
106  mLastSieveTreeWidgetItem->setText(0, serverName);
107  searchNextServerSieve();
108 }
QString userName(QUrl::ComponentFormattingOptions options) const const
QObject * sender() const const
The agent instance encountered an error state.
bool isEmpty() const const
The ManageSieveWidget class.
The SieveImapPasswordProvider class.
The SieveImapInstance class.
QVariant property(const char *name) const const
Type type(const QSqlDatabase &db)
The SieveTreeWidgetItem class.
QVariant fromValue(const T &value)
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...)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QIcon fromTheme(const QString &name)
bool setProperty(const char *name, const QVariant &value)
static SieveJob * list(const QUrl &url)
Lists all available scripts at the given sieve url.
Definition: sievejob.cpp:314
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:37
QString toString() const const
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Fri Apr 16 2021 23:09:33 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.