File indexing completed on 2024-06-23 05:18:36

0001 /*
0002     SPDX-FileCopyrightText: 2010 Casey Link <unnamedrambler@gmail.com>
0003     SPDX-FileCopyrightText: 2009-2010 Klaralvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
0004 
0005     Refactored from earlier code by:
0006     SPDX-FileCopyrightText: 2010 Volker Krause <vkrause@kde.org>
0007     SPDX-FileCopyrightText: 2004 Cornelius Schumacher <schumacher@kde.org>
0008 
0009     SPDX-License-Identifier: LGPL-2.0-or-later
0010 */
0011 
0012 #include "recipientseditorsidewidget.h"
0013 
0014 #include "kwindowpositioner.h"
0015 #include "recipientspicker.h"
0016 
0017 #include <KLocalizedString>
0018 #include <QLabel>
0019 #include <QPushButton>
0020 #include <QVBoxLayout>
0021 
0022 using namespace MessageComposer;
0023 
0024 RecipientsEditorSideWidget::RecipientsEditorSideWidget(RecipientsEditor *view, QWidget *parent)
0025     : QWidget(parent)
0026     , mEditor(view)
0027 {
0028     auto topLayout = new QVBoxLayout(this);
0029 
0030     topLayout->setContentsMargins({});
0031     topLayout->addStretch(1);
0032 
0033     mTotalLabel = new QLabel(this);
0034     mTotalLabel->setAlignment(Qt::AlignCenter);
0035     mTotalLabel->setTextFormat(Qt::PlainText);
0036     topLayout->addWidget(mTotalLabel);
0037     mTotalLabel->hide();
0038 
0039     topLayout->addStretch(1);
0040 
0041     mDistributionListButton = new QPushButton(i18nc("@action:button", "Save List..."), this);
0042     topLayout->addWidget(mDistributionListButton);
0043     mDistributionListButton->hide();
0044     connect(mDistributionListButton, &QAbstractButton::clicked, this, &RecipientsEditorSideWidget::saveDistributionList);
0045     mDistributionListButton->setToolTip(i18nc("@info:tooltip", "Save recipients as distribution list"));
0046 
0047     mSelectButton = new QPushButton(i18nc("@action:button Open recipient selection dialog.", "Se&lect..."), this);
0048     topLayout->addWidget(mSelectButton);
0049     connect(mSelectButton, &QPushButton::clicked, this, &RecipientsEditorSideWidget::pickRecipient);
0050     mSelectButton->setToolTip(i18nc("@info:tooltip", "Select recipients from address book"));
0051     updateTotalToolTip();
0052 }
0053 
0054 RecipientsEditorSideWidget::~RecipientsEditorSideWidget() = default;
0055 
0056 RecipientsPicker *RecipientsEditorSideWidget::picker() const
0057 {
0058     if (!mRecipientPicker) {
0059         // hacks to allow picker() to be const in the presence of lazy loading
0060         auto non_const_this = const_cast<RecipientsEditorSideWidget *>(this);
0061         mRecipientPicker = new RecipientsPicker(non_const_this);
0062         connect(mRecipientPicker, &RecipientsPicker::pickedRecipient, non_const_this, &RecipientsEditorSideWidget::pickedRecipient);
0063         mPickerPositioner = new KWindowPositioner(mSelectButton, mRecipientPicker);
0064     }
0065     return mRecipientPicker;
0066 }
0067 
0068 void RecipientsEditorSideWidget::setFocus()
0069 {
0070     mSelectButton->setFocus();
0071 }
0072 
0073 void RecipientsEditorSideWidget::setTotal(int recipients, int lines)
0074 {
0075     QString labelText;
0076     if (recipients == 0) {
0077         labelText = i18nc("@info:status No recipients selected", "No recipients");
0078     } else {
0079         labelText = i18ncp("@info:status Number of recipients selected", "1 recipient", "%1 recipients", recipients);
0080     }
0081 
0082     if (lines > 3) {
0083         mTotalLabel->setText(labelText);
0084         mTotalLabel->show();
0085         updateTotalToolTip();
0086     } else {
0087         mTotalLabel->hide();
0088     }
0089 
0090     if (lines > 2) {
0091         mDistributionListButton->show();
0092     } else {
0093         mDistributionListButton->hide();
0094     }
0095 }
0096 
0097 void RecipientsEditorSideWidget::updateTotalToolTip()
0098 {
0099     QString text;
0100 
0101     QString to;
0102     QString cc;
0103     QString bcc;
0104     QString replyTo;
0105 
0106     Recipient::List recipients = mEditor->recipients();
0107     Recipient::List::ConstIterator it;
0108     Recipient::List::ConstIterator end(recipients.constEnd());
0109     for (it = recipients.constBegin(); it != end; ++it) {
0110         const QString emailLine = QLatin1StringView("&nbsp;&nbsp;") + (*it)->email().toHtmlEscaped() + QLatin1StringView("<br/>");
0111         switch ((*it)->type()) {
0112         case Recipient::To:
0113             to += emailLine;
0114             break;
0115         case Recipient::Cc:
0116             cc += emailLine;
0117             break;
0118         case Recipient::Bcc:
0119             bcc += emailLine;
0120             break;
0121         case Recipient::ReplyTo:
0122             replyTo += emailLine;
0123             break;
0124         default:
0125             break;
0126         }
0127     }
0128 
0129     text += i18nc("@info:tooltip %1 list of emails", "To:%1", to);
0130     if (!cc.isEmpty()) {
0131         text += i18nc("@info:tooltip %1 list of emails", "CC:%1", cc);
0132     }
0133     if (!bcc.isEmpty()) {
0134         text += i18nc("@info:tooltip %1 list of emails", "BCC:%1", bcc);
0135     }
0136     if (!replyTo.isEmpty()) {
0137         text += i18nc("@info:tooltip %1 list of emails", "Reply-To:%1", replyTo);
0138     }
0139 
0140     mTotalLabel->setToolTip(QStringLiteral("<html><head><body>%1</body></head></html>").arg(text));
0141 }
0142 
0143 void RecipientsEditorSideWidget::pickRecipient()
0144 {
0145     MessageComposer::RecipientsPicker *p = picker();
0146     Recipient::Ptr rec = mEditor->activeRecipient();
0147     if (rec) {
0148         p->setDefaultType(rec->type());
0149         p->setRecipients(mEditor->recipients());
0150         mPickerPositioner->reposition();
0151         p->show();
0152     }
0153 }
0154 
0155 #include "moc_recipientseditorsidewidget.cpp"