Messagelib

recipientspicker.cpp
1 /*
2  SPDX-FileCopyrightText: 2010 Volker Krause <[email protected]>
3  This file was part of KMail.
4  SPDX-FileCopyrightText: 2005 Cornelius Schumacher <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8 
9 #include "recipientspicker.h"
10 #include "settings/messagecomposersettings.h"
11 
12 #include <Akonadi/Contact/EmailAddressSelectionWidget>
13 #include <Akonadi/Contact/RecipientsPickerWidget>
14 #include <KContacts/ContactGroup>
15 #include <PimCommonAkonadi/LdapSearchDialog>
16 
17 #include "messagecomposer_debug.h"
18 #include <KConfigGroup>
19 #include <KLocalizedString>
20 #include <KMessageBox>
21 #include <QLineEdit>
22 #include <QPushButton>
23 
24 #include <KSharedConfig>
25 #include <QDialogButtonBox>
26 #include <QKeyEvent>
27 #include <QLabel>
28 #include <QTreeView>
29 #include <QVBoxLayout>
30 
31 using namespace MessageComposer;
32 namespace
33 {
34 static const char myRecipientsPickerConfigGroupName[] = "RecipientsPicker";
35 }
36 RecipientsPicker::RecipientsPicker(QWidget *parent)
37  : QDialog(parent)
38  , mView(new Akonadi::RecipientsPickerWidget(true, nullptr, this))
39  , mSelectedLabel(new QLabel(this))
40 {
41  setObjectName(QStringLiteral("RecipientsPicker"));
42  setWindowTitle(i18nc("@title:window", "Select Recipient"));
43 
44  auto mainLayout = new QVBoxLayout(this);
45 
46  mainLayout->addWidget(mView);
47  mainLayout->setStretchFactor(mView, 1);
48 
49  connect(mView->view()->selectionModel(), &QItemSelectionModel::selectionChanged, this, &RecipientsPicker::slotSelectionChanged);
50  connect(mView->view(), &QAbstractItemView::doubleClicked, this, &RecipientsPicker::slotPicked);
51 
52  mainLayout->addWidget(mSelectedLabel);
53 
54  auto searchLDAPButton = new QPushButton(i18n("Search &Directory Service"), this);
55  connect(searchLDAPButton, &QPushButton::clicked, this, &RecipientsPicker::slotSearchLDAP);
56  mainLayout->addWidget(searchLDAPButton);
57 
58  KConfig config(QStringLiteral("kabldaprc"));
59  KConfigGroup group = config.group("LDAP");
60  int numHosts = group.readEntry("NumSelectedHosts", 0);
61  if (!numHosts) {
62  searchLDAPButton->setVisible(false);
63  }
64 
65  auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, this);
66  mUser1Button = new QPushButton(this);
67  buttonBox->addButton(mUser1Button, QDialogButtonBox::ActionRole);
68  mUser2Button = new QPushButton(this);
69  buttonBox->addButton(mUser2Button, QDialogButtonBox::ActionRole);
70  mUser3Button = new QPushButton(this);
71  buttonBox->addButton(mUser3Button, QDialogButtonBox::ActionRole);
72  mUser4Button = new QPushButton(this);
73  buttonBox->addButton(mUser4Button, QDialogButtonBox::ActionRole);
74 
75  connect(buttonBox, &QDialogButtonBox::rejected, this, &RecipientsPicker::reject);
76  mainLayout->addWidget(buttonBox);
77  mUser4Button->setText(i18n("Add as &Reply-To"));
78  mUser3Button->setText(i18n("Add as &To"));
79  mUser2Button->setText(i18n("Add as CC"));
80  mUser1Button->setText(i18n("Add as &BCC"));
81  connect(mUser1Button, &QPushButton::clicked, this, &RecipientsPicker::slotBccClicked);
82  connect(mUser2Button, &QPushButton::clicked, this, &RecipientsPicker::slotCcClicked);
83  connect(mUser3Button, &QPushButton::clicked, this, &RecipientsPicker::slotToClicked);
84  connect(mUser4Button, &QPushButton::clicked, this, &RecipientsPicker::slotReplyToClicked);
85 
86  mView->emailAddressSelectionWidget()->searchLineEdit()->setFocus();
87 
88  readConfig();
89 
90  slotSelectionChanged();
91 }
92 
93 RecipientsPicker::~RecipientsPicker()
94 {
95  writeConfig();
96 }
97 
98 void RecipientsPicker::updateLabel(int nbSelected)
99 {
100  if (nbSelected == 0) {
101  mSelectedLabel->setText({});
102  } else {
103  mSelectedLabel->setText(i18np("%1 selected email", "%1 selected emails", nbSelected));
104  }
105 }
106 
107 void RecipientsPicker::slotSelectionChanged()
108 {
109  const int selectedItems{mView->emailAddressSelectionWidget()->selectedAddresses().count()};
110  const bool hasSelection = (selectedItems != 0);
111  mUser1Button->setEnabled(hasSelection);
112  mUser2Button->setEnabled(hasSelection);
113  mUser3Button->setEnabled(hasSelection);
114  mUser4Button->setEnabled(hasSelection);
115  updateLabel(selectedItems);
116 }
117 
118 void RecipientsPicker::setRecipients(const Recipient::List &)
119 {
120  mView->view()->selectionModel()->clear();
121 }
122 
123 void RecipientsPicker::setDefaultType(Recipient::Type type)
124 {
125  mDefaultType = type;
126  mUser1Button->setDefault(type == Recipient::Bcc);
127  mUser2Button->setDefault(type == Recipient::Cc);
128  mUser3Button->setDefault(type == Recipient::To);
129  mUser4Button->setDefault(type == Recipient::ReplyTo);
130 }
131 
132 void RecipientsPicker::slotToClicked()
133 {
134  pick(Recipient::To);
135 }
136 
137 void RecipientsPicker::slotReplyToClicked()
138 {
139  pick(Recipient::ReplyTo);
140 }
141 
142 void RecipientsPicker::slotCcClicked()
143 {
144  pick(Recipient::Cc);
145 }
146 
147 void RecipientsPicker::slotBccClicked()
148 {
149  pick(Recipient::Bcc);
150 }
151 
152 void RecipientsPicker::slotPicked()
153 {
154  pick(mDefaultType);
155 }
156 
157 void RecipientsPicker::pick(Recipient::Type type)
158 {
159  qCDebug(MESSAGECOMPOSER_LOG) << int(type);
160 
161  const Akonadi::EmailAddressSelection::List selections = mView->emailAddressSelectionWidget()->selectedAddresses();
162 
163  const int count = selections.count();
164  if (count == 0) {
165  return;
166  }
167 
168  if (count > MessageComposerSettings::self()->maximumRecipients()) {
169  KMessageBox::sorry(this,
170  i18np("You selected 1 recipient. The maximum supported number of "
171  "recipients is %2.\nPlease adapt the selection.",
172  "You selected %1 recipients. The maximum supported number of "
173  "recipients is %2.\nPlease adapt the selection.",
174  count,
175  MessageComposerSettings::self()->maximumRecipients()));
176  return;
177  }
178 
179  bool tooManyAddress = false;
180  for (const Akonadi::EmailAddressSelection &selection : selections) {
181  Recipient recipient;
182  recipient.setType(type);
183  recipient.setEmail(selection.quotedEmail());
184 
185  Q_EMIT pickedRecipient(recipient, tooManyAddress);
186  if (tooManyAddress) {
187  KMessageBox::sorry(this, i18n("You can not add more recipients."));
188  break;
189  }
190  }
191 }
192 
193 void RecipientsPicker::keyPressEvent(QKeyEvent *event)
194 {
195  if (event->key() == Qt::Key_Escape) {
196  close();
197  }
198 
199  QDialog::keyPressEvent(event);
200 }
201 
202 void RecipientsPicker::readConfig()
203 {
204  KSharedConfig::Ptr cfg = KSharedConfig::openStateConfig();
205  const KConfigGroup group(cfg, myRecipientsPickerConfigGroupName);
206  const QSize size = group.readEntry("Size", QSize());
207  if (!size.isEmpty()) {
208  resize(size);
209  }
210 }
211 
212 void RecipientsPicker::writeConfig()
213 {
214  KSharedConfig::Ptr cfg = KSharedConfig::openStateConfig();
215  KConfigGroup group(cfg, myRecipientsPickerConfigGroupName);
216  group.writeEntry("Size", size());
217 }
218 
219 void RecipientsPicker::slotSearchLDAP()
220 {
221  if (!mLdapSearchDialog) {
222  mLdapSearchDialog = new PimCommon::LdapSearchDialog(this);
223  connect(mLdapSearchDialog, &PimCommon::LdapSearchDialog::contactsAdded, this, &RecipientsPicker::ldapSearchResult);
224  }
225 
226  mLdapSearchDialog->setSearchText(mView->emailAddressSelectionWidget()->searchLineEdit()->text());
227  mLdapSearchDialog->show();
228 }
229 
230 void RecipientsPicker::ldapSearchResult()
231 {
232  const KContacts::Addressee::List contacts = mLdapSearchDialog->selectedContacts();
233  for (const KContacts::Addressee &contact : contacts) {
234  bool tooManyAddress = false;
235  Q_EMIT pickedRecipient(Recipient(contact.fullEmail(), Recipient::Undefined), tooManyAddress);
236  if (tooManyAddress) {
237  break;
238  }
239  }
240 }
void doubleClicked(const QModelIndex &index)
void resize(int w, int h)
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
bool close()
bool isEmpty() const const
void writeEntry(const QString &key, const QVariant &value, WriteConfigFlags pFlags=Normal)
QString i18np(const char *singular, const char *plural, const TYPE &arg...)
KSharedConfigPtr config()
QSize size() const const
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void setObjectName(const QString &name)
void clicked(bool checked)
int key() const const
virtual void keyPressEvent(QKeyEvent *e) override
QString i18n(const char *text, const TYPE &arg...)
void readConfig()
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
AddresseeList List
void setWindowTitle(const QString &)
The Recipient class.
Definition: recipient.h:27
int count(const T &value) const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
virtual bool event(QEvent *event) override
T readEntry(const QString &key, const T &aDefault) const
Key_Escape
Q_EMITQ_EMIT
void sorry(QWidget *parent, const QString &text, const QString &caption=QString(), Options options=Notify)
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.