Messagelib

recipientseditorsidewidget.cpp
1/*
2 SPDX-FileCopyrightText: 2010 Casey Link <unnamedrambler@gmail.com>
3 SPDX-FileCopyrightText: 2009-2010 Klaralvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
4
5 Refactored from earlier code by:
6 SPDX-FileCopyrightText: 2010 Volker Krause <vkrause@kde.org>
7 SPDX-FileCopyrightText: 2004 Cornelius Schumacher <schumacher@kde.org>
8
9 SPDX-License-Identifier: LGPL-2.0-or-later
10*/
11
12#include "recipientseditorsidewidget.h"
13
14#include "kwindowpositioner.h"
15#include "recipientspicker.h"
16
17#include <KLocalizedString>
18#include <QLabel>
19#include <QPushButton>
20#include <QVBoxLayout>
21
22using namespace MessageComposer;
23
24RecipientsEditorSideWidget::RecipientsEditorSideWidget(RecipientsEditor *view, QWidget *parent)
25 : QWidget(parent)
26 , mEditor(view)
27 , mTotalLabel(new QLabel(this))
28 , mDistributionListButton(new QPushButton(i18nc("@action:button", "Save List..."), this))
29 , mSelectButton(new QPushButton(i18nc("@action:button Open recipient selection dialog.", "Se&lect..."), this))
30{
31 auto topLayout = new QVBoxLayout(this);
32
33 topLayout->setContentsMargins({});
34 topLayout->addStretch(1);
35
36 mTotalLabel->setAlignment(Qt::AlignCenter);
37 mTotalLabel->setTextFormat(Qt::PlainText);
38 topLayout->addWidget(mTotalLabel);
39 mTotalLabel->hide();
40
41 topLayout->addStretch(1);
42
43 topLayout->addWidget(mDistributionListButton);
44 mDistributionListButton->hide();
45 connect(mDistributionListButton, &QAbstractButton::clicked, this, &RecipientsEditorSideWidget::saveDistributionList);
46 mDistributionListButton->setToolTip(i18nc("@info:tooltip", "Save recipients as distribution list"));
47
48 topLayout->addWidget(mSelectButton);
49 connect(mSelectButton, &QPushButton::clicked, this, &RecipientsEditorSideWidget::pickRecipient);
50 mSelectButton->setToolTip(i18nc("@info:tooltip", "Select recipients from address book"));
51 updateTotalToolTip();
52}
53
54RecipientsEditorSideWidget::~RecipientsEditorSideWidget() = default;
55
56RecipientsPicker *RecipientsEditorSideWidget::picker() const
57{
58 if (!mRecipientPicker) {
59 // hacks to allow picker() to be const in the presence of lazy loading
60 auto non_const_this = const_cast<RecipientsEditorSideWidget *>(this);
61 mRecipientPicker = new RecipientsPicker(non_const_this);
62 connect(mRecipientPicker, &RecipientsPicker::pickedRecipient, non_const_this, &RecipientsEditorSideWidget::pickedRecipient);
63 mPickerPositioner = new KWindowPositioner(mSelectButton, mRecipientPicker);
64 }
65 return mRecipientPicker;
66}
67
68void RecipientsEditorSideWidget::setFocus()
69{
70 mSelectButton->setFocus();
71}
72
73void RecipientsEditorSideWidget::setTotal(int recipients, int lines)
74{
75 QString labelText;
76 if (recipients == 0) {
77 labelText = i18nc("@info:status No recipients selected", "No recipients");
78 } else {
79 labelText = i18ncp("@info:status Number of recipients selected", "1 recipient", "%1 recipients", recipients);
80 }
81
82 if (lines > 3) {
83 mTotalLabel->setText(labelText);
84 mTotalLabel->show();
85 updateTotalToolTip();
86 } else {
87 mTotalLabel->hide();
88 }
89
90 if (lines > 2) {
91 mDistributionListButton->show();
92 } else {
93 mDistributionListButton->hide();
94 }
95}
96
97void RecipientsEditorSideWidget::updateTotalToolTip()
98{
99 QString text;
100
101 QString to;
102 QString cc;
103 QString bcc;
104 QString replyTo;
105
106 Recipient::List recipients = mEditor->recipients();
109 for (it = recipients.constBegin(); it != end; ++it) {
110 const QString emailLine = QLatin1StringView("&nbsp;&nbsp;") + (*it)->email().toHtmlEscaped() + QLatin1StringView("<br/>");
111 switch ((*it)->type()) {
112 case Recipient::To:
113 to += emailLine;
114 break;
115 case Recipient::Cc:
116 cc += emailLine;
117 break;
118 case Recipient::Bcc:
119 bcc += emailLine;
120 break;
121 case Recipient::ReplyTo:
122 replyTo += emailLine;
123 break;
124 default:
125 break;
126 }
127 }
128
129 text += i18nc("@info:tooltip %1 list of emails", "To:%1", to);
130 if (!cc.isEmpty()) {
131 text += i18nc("@info:tooltip %1 list of emails", "CC:%1", cc);
132 }
133 if (!bcc.isEmpty()) {
134 text += i18nc("@info:tooltip %1 list of emails", "BCC:%1", bcc);
135 }
136 if (!replyTo.isEmpty()) {
137 text += i18nc("@info:tooltip %1 list of emails", "Reply-To:%1", replyTo);
138 }
139
140 mTotalLabel->setToolTip(QStringLiteral("<html><head><body>%1</body></head></html>").arg(text));
141}
142
143void RecipientsEditorSideWidget::pickRecipient()
144{
145 MessageComposer::RecipientsPicker *p = picker();
146 Recipient::Ptr rec = mEditor->activeRecipient();
147 if (rec) {
148 p->setDefaultType(rec->type());
149 p->setRecipients(mEditor->recipients());
150 mPickerPositioner->reposition();
151 p->show();
152 }
153}
154
155#include "moc_recipientseditorsidewidget.cpp"
The RecipientsEditor class.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18ncp(const char *context, const char *singular, const char *plural, const TYPE &arg...)
const QList< QKeySequence > & end()
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
void clicked(bool checked)
void setText(const QString &)
typedef ConstIterator
QList::const_iterator constBegin() const const
QList::const_iterator constEnd() const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
T qobject_cast(QObject *object)
bool isEmpty() const const
AlignCenter
PlainText
void hide()
void setFocus()
void show()
void setToolTip(const QString &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sun Feb 25 2024 18:37:31 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.