File indexing completed on 2024-12-01 04:36:39

0001 /*
0002    SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "createnewserverdialog.h"
0008 #include "createnewserver/createnewserverstackwidget.h"
0009 #include <KConfigGroup>
0010 #include <KLocalizedString>
0011 #include <KSharedConfig>
0012 #include <KWindowConfig>
0013 #include <QDialogButtonBox>
0014 #include <QPushButton>
0015 #include <QVBoxLayout>
0016 #include <QWindow>
0017 
0018 namespace
0019 {
0020 static const char myConfigCreateNewAccountDialogGroupName[] = "CreateNewAccountDialog";
0021 }
0022 
0023 CreateNewServerDialog::CreateNewServerDialog(QWidget *parent)
0024     : QDialog(parent)
0025     , mCreateNewServerStackWidget(new CreateNewServerStackWidget(this))
0026 {
0027     setWindowTitle(i18nc("@title:window", "Add Server"));
0028     auto mainLayout = new QVBoxLayout(this);
0029     mainLayout->setObjectName(QStringLiteral("mainLayout"));
0030 
0031     mCreateNewServerStackWidget->setObjectName(QStringLiteral("mCreateNewServerStackWidget"));
0032     mainLayout->addWidget(mCreateNewServerStackWidget);
0033 
0034     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
0035     buttonBox->setObjectName(QStringLiteral("button"));
0036     connect(buttonBox, &QDialogButtonBox::accepted, this, &CreateNewServerDialog::accept);
0037     connect(buttonBox, &QDialogButtonBox::rejected, this, &CreateNewServerDialog::reject);
0038     mainLayout->addWidget(buttonBox);
0039     readConfig();
0040     mOkButton = buttonBox->button(QDialogButtonBox::Ok);
0041     mOkButton->setEnabled(false);
0042     connect(mCreateNewServerStackWidget, &CreateNewServerStackWidget::updateOkButton, this, [this](bool state) {
0043         mOkButton->setEnabled(state);
0044     });
0045     connect(mCreateNewServerStackWidget, &CreateNewServerStackWidget::authentication, this, &CreateNewServerDialog::authentication);
0046 }
0047 
0048 CreateNewServerDialog::~CreateNewServerDialog()
0049 {
0050     writeConfig();
0051 }
0052 
0053 AccountManager::AccountManagerInfo CreateNewServerDialog::accountInfo() const
0054 {
0055     const AccountManager::AccountManagerInfo info = mCreateNewServerStackWidget->accountInfo();
0056     return info;
0057 }
0058 
0059 void CreateNewServerDialog::setAccountInfo(const AccountManager::AccountManagerInfo &info)
0060 {
0061     setWindowTitle(i18nc("@title:window", "Modify Account"));
0062     mCreateNewServerStackWidget->setAccountInfo(info);
0063 }
0064 
0065 void CreateNewServerDialog::setExistingAccountName(const QStringList &lst)
0066 {
0067     mCreateNewServerStackWidget->setExistingAccountName(lst);
0068 }
0069 
0070 void CreateNewServerDialog::readConfig()
0071 {
0072     create(); // ensure a window is created
0073     windowHandle()->resize(QSize(800, 600));
0074     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1String(myConfigCreateNewAccountDialogGroupName));
0075     KWindowConfig::restoreWindowSize(windowHandle(), group);
0076     resize(windowHandle()->size()); // workaround for QTBUG-40584
0077 }
0078 
0079 void CreateNewServerDialog::writeConfig()
0080 {
0081     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1String(myConfigCreateNewAccountDialogGroupName));
0082     KWindowConfig::saveWindowSize(windowHandle(), group);
0083 }
0084 
0085 #include "moc_createnewserverdialog.cpp"