Libksieve

multiimapvacationdialog.cpp
1 /*
2  Copyright (C) 2013-2020 Laurent Montel <[email protected]>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "multiimapvacationdialog.h"
21 #include "vacationpagewidget.h"
22 #include "multiimapvacationmanager.h"
23 
24 #include <KLocalizedString>
25 #include <KSharedConfig>
26 #include <KMessageBox>
27 #include <QTabWidget>
28 
29 #include <QTabBar>
30 #include <QApplication>
31 #include <QStackedWidget>
32 #include <QVBoxLayout>
33 #include <QLabel>
34 #include <KConfigGroup>
35 #include <QDialogButtonBox>
36 #include <QPushButton>
37 
38 using namespace KSieveUi;
39 
40 class KSieveUi::MultiImapVacationDialogPrivate
41 {
42 public:
43  MultiImapVacationDialogPrivate()
44  {
45  }
46 
48  QTabWidget *mTabWidget = nullptr;
49  QStackedWidget *mStackedWidget = nullptr;
50  MultiImapVacationManager *mVacationManager = nullptr;
51 };
52 
53 MultiImapVacationDialog::MultiImapVacationDialog(MultiImapVacationManager *manager, QWidget *parent)
54  : QDialog(parent)
55  , d(new KSieveUi::MultiImapVacationDialogPrivate)
56 {
57  d->mVacationManager = manager;
58 
59  setWindowTitle(i18nc("@title:window", "Configure \"Out of Office\" Replies"));
60  init();
61  readConfig();
62 }
63 
64 MultiImapVacationDialog::~MultiImapVacationDialog()
65 {
66  writeConfig();
67  delete d;
68 }
69 
70 void MultiImapVacationDialog::switchToServerNamePage(const QString &serverName)
71 {
72  const int nbTab(d->mTabWidget->count());
73  for (int i = 0; i < nbTab; ++i) {
74  if (d->mTabWidget->tabText(i) == serverName) {
75  d->mTabWidget->setCurrentIndex(i);
76  break;
77  }
78  }
79 }
80 
81 void MultiImapVacationDialog::reject()
82 {
83  bool canCloseDialog = true;
84  for (int i = 0; i < d->mTabWidget->count(); ++i) {
85  VacationPageWidget *vacationPage = qobject_cast<VacationPageWidget *>(d->mTabWidget->widget(i));
86  if (vacationPage) {
87  if (vacationPage->wasChanged()) {
89  this,
90  i18nc("@info", "Do you really want to cancel?"),
91  i18nc("@title:window", "Confirmation")) == KMessageBox::Yes) {
92  QDialog::reject(); // Discard current changes
93  }
94  canCloseDialog = false;
95  break;
96  }
97  }
98  }
99  if (canCloseDialog) {
100  QDialog::reject();
101  }
102 }
103 
104 QVector<VacationCreateScriptJob *> MultiImapVacationDialog::listCreateJob() const
105 {
106  return d->mListCreateJob;
107 }
108 
109 void MultiImapVacationDialog::init()
110 {
111  d->mStackedWidget = new QStackedWidget;
112  QVBoxLayout *mainLayout = new QVBoxLayout(this);
113  mainLayout->addWidget(d->mStackedWidget);
114  d->mTabWidget = new QTabWidget;
115  d->mStackedWidget->addWidget(d->mTabWidget);
116 
117  QWidget *w = new QWidget;
118  QVBoxLayout *vbox = new QVBoxLayout;
119  w->setLayout(vbox);
120  QLabel *lab = new QLabel(i18n("KMail's Out of Office Reply functionality relies on "
121  "server-side filtering. You have not yet configured an "
122  "IMAP server for this. "
123  "You can do this on the \"Filtering\" tab of the IMAP "
124  "account configuration."));
125  lab->setWordWrap(true);
127  QFont font = lab->font();
128  font.setBold(true);
129  font.setPointSize(font.pointSize() + 2);
130  lab->setFont(font);
131  vbox->addWidget(lab);
132  lab->setWordWrap(true);
133  d->mStackedWidget->addWidget(w);
134  d->mStackedWidget->setCurrentIndex(0);
135  bool foundOneImap = false;
136  QDialogButtonBox *buttonBox = nullptr;
137 
138  const QMap <QString, KSieveUi::Util::AccountInfo> list = d->mVacationManager->serverList();
140  while (i.hasNext()) {
141  i.next();
142  createPage(i.key(), i.value());
143  foundOneImap = true;
144  }
145  if (foundOneImap) {
147  QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
148  okButton->setDefault(true);
149  okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
150  connect(buttonBox, &QDialogButtonBox::accepted, this, &MultiImapVacationDialog::slotOkClicked);
151  connect(buttonBox, &QDialogButtonBox::rejected, this, &MultiImapVacationDialog::slotCanceled);
152  connect(buttonBox->button(QDialogButtonBox::RestoreDefaults), &QPushButton::clicked, this, &MultiImapVacationDialog::slotDefaultClicked);
153  } else { //Empty
154  d->mStackedWidget->setCurrentIndex(1);
155  buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, this);
156  connect(buttonBox, &QDialogButtonBox::accepted, this, &MultiImapVacationDialog::slotOkClicked);
157  connect(buttonBox, &QDialogButtonBox::rejected, this, &MultiImapVacationDialog::slotCanceled);
158  }
159  mainLayout->addWidget(buttonBox);
160  if (d->mTabWidget->count() <= 1) {
161  d->mTabWidget->tabBar()->hide();
162  }
163 }
164 
165 void MultiImapVacationDialog::slotCanceled()
166 {
167  Q_EMIT cancelClicked();
168 }
169 
170 void MultiImapVacationDialog::createPage(const QString &serverName, const KSieveUi::Util::AccountInfo &info)
171 {
172  VacationPageWidget *page = new VacationPageWidget;
173  page->setServerUrl(info.sieveUrl);
174  page->setServerName(serverName);
175  page->setVacationManager(d->mVacationManager);
176  page->setSieveImapAccountSettings(info.sieveImapAccountSettings);
177  d->mTabWidget->addTab(page, serverName + QStringLiteral(" (%1)").arg(info.sieveUrl.userName()));
178 }
179 
180 void MultiImapVacationDialog::readConfig()
181 {
182  KConfigGroup group(KSharedConfig::openConfig(), "MultiImapVacationDialog");
183  const QSize size = group.readEntry("Size", QSize());
184  if (size.isValid()) {
185  resize(size);
186  } else {
187  resize(sizeHint().width(), sizeHint().height());
188  }
189 }
190 
191 void MultiImapVacationDialog::writeConfig()
192 {
193  KConfigGroup group(KSharedConfig::openConfig(), "MultiImapVacationDialog");
194  group.writeEntry("Size", size());
195 }
196 
197 void MultiImapVacationDialog::slotOkClicked()
198 {
199  bool errorFound = false;
200  qDeleteAll(d->mListCreateJob);
201  d->mListCreateJob.clear();
202  for (int i = 0; i < d->mTabWidget->count(); ++i) {
203  VacationPageWidget *vacationPage = qobject_cast<VacationPageWidget *>(d->mTabWidget->widget(i));
204  if (vacationPage) {
205  VacationCreateScriptJob *job = vacationPage->writeScript(errorFound);
206 
207  if (job && !errorFound) {
208  d->mListCreateJob.append(job);
209  }
210  if (errorFound) {
211  //Clean up job
212  qDeleteAll(d->mListCreateJob);
213  d->mListCreateJob.clear();
214  break;
215  }
216  }
217  }
218  if (!errorFound) {
219  Q_EMIT okClicked();
220  }
221 }
222 
223 void MultiImapVacationDialog::slotDefaultClicked()
224 {
225  for (int i = 0; i < d->mTabWidget->count(); ++i) {
226  VacationPageWidget *vacationPage = qobject_cast<VacationPageWidget *>(d->mTabWidget->widget(i));
227  if (vacationPage) {
228  vacationPage->setDefault();
229  }
230  }
231 }
void resize(int w, int h)
bool isValid() const const
void setPointSize(int pointSize)
virtual void reject()
void setAlignment(Qt::Alignment)
AlignHCenter
The MultiImapVacationManager class.
int width() const const
void setBold(bool enable)
QSize size() const const
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
void setLayout(QLayout *layout)
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
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
QCA_EXPORT void init()
QString i18n(const char *text, const TYPE &arg...)
void readConfig()
void setWindowTitle(const QString &)
QWidget(QWidget *parent, Qt::WindowFlags f)
QPushButton * button(QDialogButtonBox::StandardButton which) const const
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)
void setWordWrap(bool on)
Key_Return
int pointSize() const const
int height() const const
Q_EMITQ_EMIT
KIOFILEWIDGETS_EXPORT QStringList list(const QString &fileClass)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Thu Jul 9 2020 23:07:44 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.