Libksieve

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

KDE's Doxygen guidelines are available online.