File indexing completed on 2024-12-15 04:55:37

0001 /*
0002    SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "serversievesettingsdialog.h"
0008 #include "serversievesettings.h"
0009 
0010 #include <KLocalizedString>
0011 #include <KWindowConfig>
0012 #include <QDialogButtonBox>
0013 #include <QPushButton>
0014 #include <QVBoxLayout>
0015 #include <QWindow>
0016 
0017 namespace
0018 {
0019 static const char myServerSieveSettingsDialog[] = "ServerSieveSettingsDialog";
0020 }
0021 
0022 ServerSieveSettingsDialog::ServerSieveSettingsDialog(QWidget *parent)
0023     : QDialog(parent)
0024 {
0025     setWindowTitle(i18nc("@title:window", "Add Sieve Server"));
0026 
0027     auto w = new QWidget;
0028     auto lay = new QVBoxLayout;
0029     mServerSieveSettings = new ServerSieveSettings;
0030     connect(mServerSieveSettings, &ServerSieveSettings::enableOkButton, this, &ServerSieveSettingsDialog::slotEnableButtonOk);
0031     lay->addWidget(mServerSieveSettings);
0032     lay->setContentsMargins({});
0033     w->setLayout(lay);
0034     auto mainLayout = new QVBoxLayout(this);
0035     mainLayout->addWidget(w);
0036 
0037     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
0038     mOkButton = buttonBox->button(QDialogButtonBox::Ok);
0039     mOkButton->setDefault(true);
0040     mOkButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0041     connect(buttonBox, &QDialogButtonBox::accepted, this, &ServerSieveSettingsDialog::accept);
0042     connect(buttonBox, &QDialogButtonBox::rejected, this, &ServerSieveSettingsDialog::reject);
0043     mainLayout->addWidget(buttonBox);
0044 
0045     readConfig();
0046     mOkButton->setEnabled(false);
0047 }
0048 
0049 ServerSieveSettingsDialog::~ServerSieveSettingsDialog()
0050 {
0051     writeConfig();
0052 }
0053 
0054 void ServerSieveSettingsDialog::readConfig()
0055 {
0056     create(); // ensure a window is created
0057     windowHandle()->resize(QSize(450, 350));
0058     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myServerSieveSettingsDialog));
0059     KWindowConfig::restoreWindowSize(windowHandle(), group);
0060     resize(windowHandle()->size()); // workaround for QTBUG-40584
0061 }
0062 
0063 void ServerSieveSettingsDialog::writeConfig()
0064 {
0065     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myServerSieveSettingsDialog));
0066     KWindowConfig::saveWindowSize(windowHandle(), group);
0067     group.sync();
0068 }
0069 
0070 void ServerSieveSettingsDialog::slotEnableButtonOk(bool b)
0071 {
0072     mOkButton->setEnabled(b);
0073 }
0074 
0075 QString ServerSieveSettingsDialog::serverName() const
0076 {
0077     return mServerSieveSettings->serverName();
0078 }
0079 
0080 void ServerSieveSettingsDialog::setServerName(const QString &name)
0081 {
0082     mServerSieveSettings->setServerName(name);
0083 }
0084 
0085 int ServerSieveSettingsDialog::port() const
0086 {
0087     return mServerSieveSettings->port();
0088 }
0089 
0090 void ServerSieveSettingsDialog::setPort(int value)
0091 {
0092     mServerSieveSettings->setPort(value);
0093 }
0094 
0095 QString ServerSieveSettingsDialog::userName() const
0096 {
0097     return mServerSieveSettings->userName();
0098 }
0099 
0100 void ServerSieveSettingsDialog::setUserName(const QString &name)
0101 {
0102     mServerSieveSettings->setUserName(name);
0103 }
0104 
0105 QString ServerSieveSettingsDialog::password() const
0106 {
0107     return mServerSieveSettings->password();
0108 }
0109 
0110 void ServerSieveSettingsDialog::setPassword(const QString &pass)
0111 {
0112     mServerSieveSettings->setPassword(pass);
0113 }
0114 
0115 void ServerSieveSettingsDialog::setServerSieveConfig(const SieveEditorUtil::SieveServerConfig &conf)
0116 {
0117     mServerSieveSettings->setServerSieveConfig(conf);
0118 }
0119 
0120 SieveEditorUtil::SieveServerConfig ServerSieveSettingsDialog::serverSieveConfig() const
0121 {
0122     return mServerSieveSettings->serverSieveConfig();
0123 }
0124 
0125 #include "moc_serversievesettingsdialog.cpp"