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

0001 /*
0002    SPDX-FileCopyrightText: 2017-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "selectmaildialog.h"
0008 #include "selectmailwidget.h"
0009 #include <KConfigGroup>
0010 #include <KSharedConfig>
0011 #include <KWindowConfig>
0012 #include <QDialogButtonBox>
0013 #include <QVBoxLayout>
0014 #include <QWindow>
0015 
0016 namespace
0017 {
0018 static const char myConfigGroupName[] = "SelectMailDialog";
0019 }
0020 SelectMailDialog::SelectMailDialog(QWidget *parent)
0021     : QDialog(parent)
0022     , mSelectMailWidget(new SelectMailWidget(this))
0023 {
0024     auto mainLayout = new QVBoxLayout(this);
0025 
0026     mSelectMailWidget->setObjectName(QLatin1StringView("selectmailwidget"));
0027     mainLayout->addWidget(mSelectMailWidget);
0028     connect(mSelectMailWidget, &SelectMailWidget::doubleClicked, this, &SelectMailDialog::slotInsertEmails);
0029 
0030     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
0031     buttonBox->setObjectName(QLatin1StringView("buttonbox"));
0032     connect(buttonBox, &QDialogButtonBox::accepted, this, &SelectMailDialog::accept);
0033     connect(buttonBox, &QDialogButtonBox::rejected, this, &SelectMailDialog::reject);
0034     mainLayout->addWidget(buttonBox);
0035     readConfig();
0036 }
0037 
0038 SelectMailDialog::~SelectMailDialog()
0039 {
0040     writeConfig();
0041 }
0042 
0043 QStringList SelectMailDialog::selectedEmails() const
0044 {
0045     return mSelectMailWidget->selectedEmails();
0046 }
0047 
0048 void SelectMailDialog::slotInsertEmails()
0049 {
0050     Q_EMIT emailSelected(selectedEmails());
0051 }
0052 
0053 void SelectMailDialog::accept()
0054 {
0055     Q_EMIT emailSelected(selectedEmails());
0056     QDialog::accept();
0057 }
0058 
0059 void SelectMailDialog::readConfig()
0060 {
0061     create(); // ensure a window is created
0062     windowHandle()->resize(QSize(300, 200));
0063     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myConfigGroupName));
0064     KWindowConfig::restoreWindowSize(windowHandle(), group);
0065     resize(windowHandle()->size()); // workaround for QTBUG-40584
0066 }
0067 
0068 void SelectMailDialog::writeConfig()
0069 {
0070     KConfigGroup grp(KSharedConfig::openStateConfig(), QLatin1StringView(myConfigGroupName));
0071     KWindowConfig::saveWindowSize(windowHandle(), grp);
0072     grp.sync();
0073 }
0074 
0075 #include "moc_selectmaildialog.cpp"