Messagelib

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

KDE's Doxygen guidelines are available online.