File indexing completed on 2025-01-05 04:49:50

0001 /*
0002    SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "selectimapfolderdialog.h"
0008 #include "selectimapfolderwidget.h"
0009 
0010 #include <KConfigGroup>
0011 #include <KLocalizedString>
0012 #include <KSharedConfig>
0013 
0014 #include <KWindowConfig>
0015 #include <QDialogButtonBox>
0016 #include <QPushButton>
0017 #include <QVBoxLayout>
0018 #include <QWindow>
0019 namespace
0020 {
0021 static const char myConfigSelectImapFolderDialog[] = "SelectImapFolderDialog";
0022 }
0023 
0024 SelectImapFolderDialog::SelectImapFolderDialog(const KSieveCore::SieveImapAccountSettings &account, QWidget *parent)
0025     : QDialog(parent)
0026     , mSelectImapFolderWidget(new SelectImapFolderWidget(account, this))
0027     , mNewFolder(new QPushButton(i18n("Create Folder..."), this))
0028 {
0029     setWindowTitle(i18nc("@title:window", "Select IMAP folder"));
0030     auto layout = new QVBoxLayout(this);
0031 
0032     mSelectImapFolderWidget->setObjectName(QLatin1StringView("selectimapfolderwidget"));
0033     layout->addWidget(mSelectImapFolderWidget);
0034     connect(mSelectImapFolderWidget, &SelectImapFolderWidget::folderSelected, this, &SelectImapFolderDialog::accept);
0035     connect(mSelectImapFolderWidget, &SelectImapFolderWidget::folderIsSelected, this, &SelectImapFolderDialog::slotEnabledNewFolder);
0036     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
0037     layout->addWidget(buttonBox);
0038     buttonBox->setObjectName(QLatin1StringView("buttonbox"));
0039 
0040     mNewFolder->setObjectName(QLatin1StringView("createfolder"));
0041     mNewFolder->setEnabled(false);
0042     buttonBox->addButton(mNewFolder, QDialogButtonBox::ActionRole);
0043     connect(mNewFolder, &QPushButton::clicked, this, &SelectImapFolderDialog::slotCreateFolder);
0044 
0045     // Set the button actions
0046     connect(buttonBox, &QDialogButtonBox::accepted, this, &SelectImapFolderDialog::accept);
0047     connect(buttonBox, &QDialogButtonBox::rejected, this, &SelectImapFolderDialog::reject);
0048     readConfig();
0049 }
0050 
0051 SelectImapFolderDialog::~SelectImapFolderDialog()
0052 {
0053     writeConfig();
0054 }
0055 
0056 QString SelectImapFolderDialog::selectedFolderName() const
0057 {
0058     return mSelectImapFolderWidget->selectedFolderName();
0059 }
0060 
0061 void SelectImapFolderDialog::readConfig()
0062 {
0063     create(); // ensure a window is created
0064     windowHandle()->resize(QSize(500, 300));
0065     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myConfigSelectImapFolderDialog));
0066     KWindowConfig::restoreWindowSize(windowHandle(), group);
0067     resize(windowHandle()->size()); // workaround for QTBUG-40584
0068 }
0069 
0070 void SelectImapFolderDialog::writeConfig()
0071 {
0072     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myConfigSelectImapFolderDialog));
0073     KWindowConfig::saveWindowSize(windowHandle(), group);
0074     group.sync();
0075 }
0076 
0077 void SelectImapFolderDialog::slotCreateFolder()
0078 {
0079     mSelectImapFolderWidget->createFolder();
0080 }
0081 
0082 void SelectImapFolderDialog::slotEnabledNewFolder(bool enabled)
0083 {
0084     mNewFolder->setEnabled(enabled);
0085 }
0086 
0087 #include "moc_selectimapfolderdialog.cpp"