Libksieve

multiimapvacationdialog.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "multiimapvacationdialog.h"
8 #include "multiimapvacationmanager.h"
9 #include "vacation/searchserverwithvacationsupportjob.h"
10 #include "vacationpagewidget.h"
11 
12 #include <KLocalizedString>
13 #include <KMessageBox>
14 #include <KSharedConfig>
15 #include <QTabWidget>
16 
17 #include <KConfigGroup>
18 #include <QDialogButtonBox>
19 #include <QLabel>
20 #include <QPushButton>
21 #include <QStackedWidget>
22 #include <QTabBar>
23 #include <QVBoxLayout>
24 
25 using namespace KSieveUi;
26 
27 class KSieveUi::MultiImapVacationDialogPrivate
28 {
29 public:
30  MultiImapVacationDialogPrivate()
31  {
32  }
33 
35  QTabWidget *mTabWidget = nullptr;
36  QStackedWidget *mStackedWidget = nullptr;
37  MultiImapVacationManager *mVacationManager = nullptr;
38 };
39 
40 MultiImapVacationDialog::MultiImapVacationDialog(MultiImapVacationManager *manager, QWidget *parent)
41  : QDialog(parent)
42  , d(new KSieveUi::MultiImapVacationDialogPrivate)
43 {
44  d->mVacationManager = manager;
45 
46  setWindowTitle(i18nc("@title:window", "Configure \"Out of Office\" Replies"));
47  init();
48  readConfig();
49 }
50 
51 MultiImapVacationDialog::~MultiImapVacationDialog()
52 {
53  writeConfig();
54 }
55 
56 void MultiImapVacationDialog::switchToServerNamePage(const QString &serverName)
57 {
58  const int nbTab(d->mTabWidget->count());
59  for (int i = 0; i < nbTab; ++i) {
60  if (d->mTabWidget->tabText(i) == serverName) {
61  d->mTabWidget->setCurrentIndex(i);
62  break;
63  }
64  }
65 }
66 
67 void MultiImapVacationDialog::reject()
68 {
69  bool canCloseDialog = true;
70  for (int i = 0; i < d->mTabWidget->count(); ++i) {
71  auto vacationPage = qobject_cast<VacationPageWidget *>(d->mTabWidget->widget(i));
72  if (vacationPage) {
73  if (vacationPage->wasChanged()) {
74  if (KMessageBox::questionYesNo(this, i18nc("@info", "Do you really want to cancel?"), i18nc("@title:window", "Confirmation"))
75  == KMessageBox::Yes) {
76  QDialog::reject(); // Discard current changes
77  }
78  canCloseDialog = false;
79  break;
80  }
81  }
82  }
83  if (canCloseDialog) {
85  }
86 }
87 
88 QVector<VacationCreateScriptJob *> MultiImapVacationDialog::listCreateJob() const
89 {
90  return d->mListCreateJob;
91 }
92 
93 void MultiImapVacationDialog::init()
94 {
95  d->mStackedWidget = new QStackedWidget;
96  auto mainLayout = new QVBoxLayout(this);
97  mainLayout->addWidget(d->mStackedWidget);
98  d->mTabWidget = new QTabWidget;
99  d->mStackedWidget->addWidget(d->mTabWidget);
100 
101  auto w = new QWidget;
102  auto vbox = new QVBoxLayout;
103  w->setLayout(vbox);
104  auto lab =
105  new QLabel(i18n("KMail's Out of Office Reply functionality relies on "
106  "server-side filtering. You have not yet configured an "
107  "IMAP server for this. "
108  "You can do this on the \"Filtering\" tab of the IMAP "
109  "account configuration."));
110  lab->setWordWrap(true);
111  lab->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
112  QFont font = lab->font();
113  font.setBold(true);
114  font.setPointSize(font.pointSize() + 2);
115  lab->setFont(font);
116  vbox->addWidget(lab);
117  lab->setWordWrap(true);
118  d->mStackedWidget->addWidget(w);
119  d->mStackedWidget->setCurrentIndex(0);
120  mButtonBox = new QDialogButtonBox(this);
121  mButtonBox->setObjectName(QStringLiteral("mButtonBox"));
122  mainLayout->addWidget(mButtonBox);
123  initialize();
124 }
125 
126 void MultiImapVacationDialog::initialize()
127 {
128  auto job = new SearchServerWithVacationSupportJob(this);
129  job->setPasswordProvider(d->mVacationManager->passwordProvider());
130  connect(job,
131  &SearchServerWithVacationSupportJob::searchServerWithVacationSupportFinished,
132  this,
133  &MultiImapVacationDialog::slotSearchServerWithVacationSupportFinished);
134  job->start();
135 }
136 
137 void MultiImapVacationDialog::slotSearchServerWithVacationSupportFinished(const QMap<QString, KSieveUi::Util::AccountInfo> &list)
138 {
139  bool foundOneImap = false;
140 
142  while (i.hasNext()) {
143  i.next();
144  createPage(i.key(), i.value());
145  foundOneImap = true;
146  }
147  if (foundOneImap) {
149  QPushButton *okButton = mButtonBox->button(QDialogButtonBox::Ok);
150  okButton->setDefault(true);
151  okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
152  connect(mButtonBox, &QDialogButtonBox::accepted, this, &MultiImapVacationDialog::slotOkClicked);
153  connect(mButtonBox, &QDialogButtonBox::rejected, this, &MultiImapVacationDialog::slotCanceled);
154  connect(mButtonBox->button(QDialogButtonBox::RestoreDefaults), &QPushButton::clicked, this, &MultiImapVacationDialog::slotDefaultClicked);
155  } else { // Empty
156  d->mStackedWidget->setCurrentIndex(1);
157  mButtonBox->setStandardButtons(QDialogButtonBox::Close);
158  connect(mButtonBox, &QDialogButtonBox::accepted, this, &MultiImapVacationDialog::slotOkClicked);
159  connect(mButtonBox, &QDialogButtonBox::rejected, this, &MultiImapVacationDialog::slotCanceled);
160  }
161  if (d->mTabWidget->count() <= 1) {
162  d->mTabWidget->tabBar()->hide();
163  }
164 }
165 
166 void MultiImapVacationDialog::slotCanceled()
167 {
168  Q_EMIT cancelClicked();
169 }
170 
171 void MultiImapVacationDialog::createPage(const QString &serverName, const KSieveUi::Util::AccountInfo &info)
172 {
173  auto page = new VacationPageWidget;
174  page->setServerUrl(info.sieveUrl);
175  page->setServerName(serverName);
176  page->setVacationManager(d->mVacationManager);
177  page->setSieveImapAccountSettings(info.sieveImapAccountSettings);
178  d->mTabWidget->addTab(page, serverName + QStringLiteral(" (%1)").arg(info.sieveUrl.userName()));
179 }
180 
181 void MultiImapVacationDialog::readConfig()
182 {
183  KConfigGroup group(KSharedConfig::openStateConfig(), "MultiImapVacationDialog");
184  const QSize size = group.readEntry("Size", QSize());
185  if (size.isValid()) {
186  resize(size);
187  } else {
188  resize(sizeHint().width(), sizeHint().height());
189  }
190 }
191 
192 void MultiImapVacationDialog::writeConfig()
193 {
194  KConfigGroup group(KSharedConfig::openStateConfig(), "MultiImapVacationDialog");
195  group.writeEntry("Size", size());
196 }
197 
198 void MultiImapVacationDialog::slotOkClicked()
199 {
200  bool errorFound = false;
201  qDeleteAll(d->mListCreateJob);
202  d->mListCreateJob.clear();
203  for (int i = 0; i < d->mTabWidget->count(); ++i) {
204  auto vacationPage = qobject_cast<VacationPageWidget *>(d->mTabWidget->widget(i));
205  if (vacationPage) {
206  VacationCreateScriptJob *job = vacationPage->writeScript(errorFound);
207 
208  if (job && !errorFound) {
209  d->mListCreateJob.append(job);
210  }
211  if (errorFound) {
212  // Clean up job
213  qDeleteAll(d->mListCreateJob);
214  d->mListCreateJob.clear();
215  break;
216  }
217  }
218  }
219  if (!errorFound) {
220  Q_EMIT okClicked();
221  }
222 }
223 
224 void MultiImapVacationDialog::slotDefaultClicked()
225 {
226  for (int i = 0; i < d->mTabWidget->count(); ++i) {
227  auto vacationPage = qobject_cast<VacationPageWidget *>(d->mTabWidget->widget(i));
228  if (vacationPage) {
229  vacationPage->setDefault();
230  }
231  }
232 }
void resize(int w, int h)
bool isValid() const const
void setPointSize(int pointSize)
virtual void reject()
KCRASH_EXPORT void initialize()
AlignHCenter
The MultiImapVacationManager class.
int width() const const
void setBold(bool enable)
QSize size() const const
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void clicked(bool checked)
void setShortcut(const QKeySequence &key)
ButtonCode questionYesNo(QWidget *parent, const QString &text, const QString &caption=QString(), const KGuiItem &buttonYes=KStandardGuiItem::yes(), const KGuiItem &buttonNo=KStandardGuiItem::no(), const QString &dontAskAgainName=QString(), Options options=Notify)
const QFont & font() const const
virtual QSize sizeHint() const const
QCA_EXPORT void init()
QString i18n(const char *text, const TYPE &arg...)
void readConfig()
void setWindowTitle(const QString &)
QWidget(QWidget *parent, Qt::WindowFlags f)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
T qobject_cast(QObject *object)
void setDefault(bool)
Key_Return
int pointSize() const const
int height() const const
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Apr 11 2021 23:09:36 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.