Warning, file /pim/mailcommon/src/widgets/redirectdialog.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002   SPDX-FileCopyrightText: 2003 Andreas Gungl <a.gungl@gmx.de>
0003   SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org>
0004 
0005   SPDX-License-Identifier: GPL-2.0-only
0006 */
0007 
0008 #include "redirectdialog.h"
0009 #include "kernel/mailkernel.h"
0010 
0011 #include "redirectwidget.h"
0012 
0013 #include <KIdentityManagementCore/Identity>
0014 #include <KIdentityManagementWidgets/IdentityCombo>
0015 
0016 #include <MailTransport/TransportComboBox>
0017 
0018 #include <KLocalizedString>
0019 #include <KMessageBox>
0020 
0021 #include <KGuiItem>
0022 #include <QDialogButtonBox>
0023 #include <QFormLayout>
0024 #include <QLabel>
0025 #include <QPushButton>
0026 #include <QTreeView>
0027 #include <QVBoxLayout>
0028 
0029 using namespace MailCommon;
0030 
0031 class Q_DECL_HIDDEN RedirectDialog::RedirectDialogPrivate
0032 {
0033 public:
0034     RedirectDialogPrivate(RedirectDialog *qq, RedirectDialog::SendMode mode)
0035         : q(qq)
0036         , mSendMode(mode)
0037     {
0038     }
0039 
0040     enum TypeAddress {
0041         ResendTo,
0042         ResendCc,
0043         ResendBcc,
0044     };
0045 
0046     void slotUser1();
0047     void slotUser2();
0048     void slotAddressChanged(const QString &);
0049     [[nodiscard]] QString redirectLabelType(TypeAddress type) const;
0050     RedirectDialog *const q = nullptr;
0051     RedirectWidget *mEditTo = nullptr;
0052     RedirectWidget *mEditCc = nullptr;
0053     RedirectWidget *mEditBcc = nullptr;
0054 
0055     RedirectDialog::SendMode mSendMode;
0056     KIdentityManagementWidgets::IdentityCombo *mComboboxIdentity = nullptr;
0057     MailTransport::TransportComboBox *mTransportCombobox = nullptr;
0058     QPushButton *mUser1Button = nullptr;
0059     QPushButton *mUser2Button = nullptr;
0060 };
0061 
0062 QString RedirectDialog::RedirectDialogPrivate::redirectLabelType(TypeAddress type) const
0063 {
0064     QString label;
0065     switch (type) {
0066     case ResendTo:
0067         label = i18n("Resend-To:");
0068         break;
0069     case ResendCc:
0070         label = i18n("Resend-Cc:");
0071         break;
0072     case ResendBcc:
0073         label = i18n("Resend-Bcc:");
0074         break;
0075     }
0076     return label;
0077 }
0078 
0079 void RedirectDialog::RedirectDialogPrivate::slotUser1()
0080 {
0081     mSendMode = RedirectDialog::SendNow;
0082     q->accept();
0083 }
0084 
0085 void RedirectDialog::RedirectDialogPrivate::slotUser2()
0086 {
0087     mSendMode = RedirectDialog::SendLater;
0088     q->accept();
0089 }
0090 
0091 void RedirectDialog::RedirectDialogPrivate::slotAddressChanged(const QString &text)
0092 {
0093     const bool textIsNotEmpty(!text.trimmed().isEmpty());
0094     mUser1Button->setEnabled(textIsNotEmpty);
0095     mUser2Button->setEnabled(textIsNotEmpty);
0096 }
0097 
0098 RedirectDialog::RedirectDialog(SendMode mode, QWidget *parent)
0099     : QDialog(parent)
0100     , d(new RedirectDialogPrivate(this, mode))
0101 {
0102     setWindowTitle(i18nc("@title:window", "Redirect Message"));
0103     auto topLayout = new QVBoxLayout(this);
0104     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Cancel, this);
0105     d->mUser1Button = new QPushButton(this);
0106     buttonBox->addButton(d->mUser1Button, QDialogButtonBox::ActionRole);
0107     d->mUser2Button = new QPushButton(this);
0108     buttonBox->addButton(d->mUser2Button, QDialogButtonBox::ActionRole);
0109     connect(buttonBox, &QDialogButtonBox::rejected, this, &RedirectDialog::reject);
0110     if (mode == SendNow) {
0111         d->mUser1Button->setDefault(true);
0112     } else {
0113         d->mUser2Button->setDefault(true);
0114     }
0115 
0116     auto mainWidget = new QWidget;
0117     topLayout->addWidget(mainWidget);
0118     topLayout->addWidget(buttonBox);
0119 
0120     auto mainLayout = new QVBoxLayout;
0121     mainWidget->setLayout(mainLayout);
0122     mainLayout->setContentsMargins({});
0123     auto LabelTo = new QLabel(i18n("Select the recipient addresses to redirect to:"));
0124     mainLayout->addWidget(LabelTo);
0125 
0126     auto formLayout = new QFormLayout;
0127     // Combo boxes look better when they are the same size as other full width fields.
0128     formLayout->setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow);
0129     // This is set to zero to avoid double margins between the
0130     // three address fields, due to their address selection buttons.
0131     formLayout->setVerticalSpacing(0);
0132     mainLayout->addLayout(formLayout);
0133 
0134     d->mEditTo = new RedirectWidget;
0135     formLayout->addRow(d->redirectLabelType(RedirectDialog::RedirectDialogPrivate::ResendTo), d->mEditTo);
0136 
0137     connect(d->mEditTo, &RedirectWidget::addressChanged, this, [this](const QString &str) {
0138         d->slotAddressChanged(str);
0139     });
0140 
0141     d->mEditCc = new RedirectWidget;
0142     formLayout->addRow(d->redirectLabelType(RedirectDialog::RedirectDialogPrivate::ResendCc), d->mEditCc);
0143     d->mEditBcc = new RedirectWidget;
0144     formLayout->addRow(d->redirectLabelType(RedirectDialog::RedirectDialogPrivate::ResendBcc), d->mEditBcc);
0145     d->mEditTo->setFocus();
0146 
0147     // Because the form layout vertical spacing was set to zero above,
0148     // it is necessary to explicitly add spacing between the combo boxes.
0149     // The layout default spacing is copied from the main layout.
0150     formLayout->addItem(new QSpacerItem(1, mainLayout->spacing(), QSizePolicy::Fixed, QSizePolicy::Fixed));
0151     d->mComboboxIdentity = new KIdentityManagementWidgets::IdentityCombo(KernelIf->identityManager());
0152     formLayout->addRow(i18n("Identity:"), d->mComboboxIdentity);
0153 
0154     formLayout->addItem(new QSpacerItem(1, mainLayout->spacing(), QSizePolicy::Fixed, QSizePolicy::Fixed));
0155     d->mTransportCombobox = new MailTransport::TransportComboBox;
0156     formLayout->addRow(i18n("Transport:"), d->mTransportCombobox);
0157 
0158     KGuiItem::assign(d->mUser1Button, KGuiItem(i18n("&Send Now"), QIcon::fromTheme(QStringLiteral("mail-send"))));
0159     KGuiItem::assign(d->mUser2Button, KGuiItem(i18n("Send &Later"), QIcon::fromTheme(QStringLiteral("mail-queue"))));
0160     connect(d->mUser1Button, &QPushButton::clicked, this, [this]() {
0161         d->slotUser1();
0162     });
0163     connect(d->mUser2Button, &QPushButton::clicked, this, [this]() {
0164         d->slotUser2();
0165     });
0166 
0167     d->mUser1Button->setEnabled(false);
0168     d->mUser2Button->setEnabled(false);
0169 }
0170 
0171 RedirectDialog::~RedirectDialog() = default;
0172 
0173 QString RedirectDialog::to() const
0174 {
0175     return d->mEditTo->resend();
0176 }
0177 
0178 QString RedirectDialog::cc() const
0179 {
0180     return d->mEditCc->resend();
0181 }
0182 
0183 QString RedirectDialog::bcc() const
0184 {
0185     return d->mEditBcc->resend();
0186 }
0187 
0188 RedirectDialog::SendMode RedirectDialog::sendMode() const
0189 {
0190     return d->mSendMode;
0191 }
0192 
0193 int RedirectDialog::transportId() const
0194 {
0195     return d->mTransportCombobox->currentTransportId();
0196 }
0197 
0198 int RedirectDialog::identity() const
0199 {
0200     return static_cast<int>(d->mComboboxIdentity->currentIdentity());
0201 }
0202 
0203 void RedirectDialog::accept()
0204 {
0205     const QString editTo = d->mEditTo->resend();
0206     if (editTo.isEmpty()) {
0207         KMessageBox::error(this, i18n("You cannot redirect the message without an address."), i18n("Empty Redirection Address"));
0208     } else {
0209         done(QDialog::Accepted);
0210     }
0211 }
0212 
0213 #include "moc_redirectdialog.cpp"