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/EmailAddressSelectionWidget>
13 #include <Akonadi/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 <KWindowConfig>
22 #include <QLineEdit>
23 #include <QPushButton>
24 #include <QWindow>
25 
26 #include <KSharedConfig>
27 #include <QDialogButtonBox>
28 #include <QKeyEvent>
29 #include <QLabel>
30 #include <QTreeView>
31 #include <QVBoxLayout>
32 
33 using namespace MessageComposer;
34 namespace
35 {
36 static const char myRecipientsPickerConfigGroupName[] = "RecipientsPicker";
37 }
38 RecipientsPicker::RecipientsPicker(QWidget *parent)
39  : QDialog(parent)
40  , mView(new Akonadi::RecipientsPickerWidget(true, nullptr, this))
41  , mSelectedLabel(new QLabel(this))
42 {
43  setObjectName(QStringLiteral("RecipientsPicker"));
44  setWindowTitle(i18nc("@title:window", "Select Recipient"));
45 
46  auto mainLayout = new QVBoxLayout(this);
47 
48  mainLayout->addWidget(mView);
49  mainLayout->setStretchFactor(mView, 1);
50 
51  connect(mView->view()->selectionModel(), &QItemSelectionModel::selectionChanged, this, &RecipientsPicker::slotSelectionChanged);
52  connect(mView->view(), &QAbstractItemView::doubleClicked, this, &RecipientsPicker::slotPicked);
53 
54  mainLayout->addWidget(mSelectedLabel);
55 
56  auto searchLDAPButton = new QPushButton(i18n("Search &Directory Service"), this);
57  connect(searchLDAPButton, &QPushButton::clicked, this, &RecipientsPicker::slotSearchLDAP);
58  mainLayout->addWidget(searchLDAPButton);
59 
60  KConfig config(QStringLiteral("kabldaprc"));
61  KConfigGroup group = config.group("LDAP");
62  int numHosts = group.readEntry("NumSelectedHosts", 0);
63  if (!numHosts) {
64  searchLDAPButton->setVisible(false);
65  }
66 
67  auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, this);
68  mUser1Button = new QPushButton(this);
69  buttonBox->addButton(mUser1Button, QDialogButtonBox::ActionRole);
70  mUser2Button = new QPushButton(this);
71  buttonBox->addButton(mUser2Button, QDialogButtonBox::ActionRole);
72  mUser3Button = new QPushButton(this);
73  buttonBox->addButton(mUser3Button, QDialogButtonBox::ActionRole);
74  mUser4Button = new QPushButton(this);
75  buttonBox->addButton(mUser4Button, QDialogButtonBox::ActionRole);
76 
77  connect(buttonBox, &QDialogButtonBox::rejected, this, &RecipientsPicker::reject);
78  mainLayout->addWidget(buttonBox);
79  mUser4Button->setText(i18n("Add as &Reply-To"));
80  mUser3Button->setText(i18n("Add as &To"));
81  mUser2Button->setText(i18n("Add as CC"));
82  mUser1Button->setText(i18n("Add as &BCC"));
83  connect(mUser1Button, &QPushButton::clicked, this, &RecipientsPicker::slotBccClicked);
84  connect(mUser2Button, &QPushButton::clicked, this, &RecipientsPicker::slotCcClicked);
85  connect(mUser3Button, &QPushButton::clicked, this, &RecipientsPicker::slotToClicked);
86  connect(mUser4Button, &QPushButton::clicked, this, &RecipientsPicker::slotReplyToClicked);
87 
88  mView->emailAddressSelectionWidget()->searchLineEdit()->setFocus();
89 
90  readConfig();
91 
92  slotSelectionChanged();
93 }
94 
95 RecipientsPicker::~RecipientsPicker()
96 {
97  writeConfig();
98 }
99 
100 void RecipientsPicker::updateLabel(int nbSelected)
101 {
102  if (nbSelected == 0) {
103  mSelectedLabel->setText({});
104  } else {
105  mSelectedLabel->setText(i18np("%1 selected email", "%1 selected emails", nbSelected));
106  }
107 }
108 
109 void RecipientsPicker::slotSelectionChanged()
110 {
111  const auto selectedItems{mView->emailAddressSelectionWidget()->selectedAddresses().count()};
112  const bool hasSelection = (selectedItems != 0);
113  mUser1Button->setEnabled(hasSelection);
114  mUser2Button->setEnabled(hasSelection);
115  mUser3Button->setEnabled(hasSelection);
116  mUser4Button->setEnabled(hasSelection);
117  updateLabel(selectedItems);
118 }
119 
120 void RecipientsPicker::setRecipients(const Recipient::List &)
121 {
122  mView->view()->selectionModel()->clear();
123 }
124 
125 void RecipientsPicker::setDefaultType(Recipient::Type type)
126 {
127  mDefaultType = type;
128  mUser1Button->setDefault(type == Recipient::Bcc);
129  mUser2Button->setDefault(type == Recipient::Cc);
130  mUser3Button->setDefault(type == Recipient::To);
131  mUser4Button->setDefault(type == Recipient::ReplyTo);
132 }
133 
134 void RecipientsPicker::slotToClicked()
135 {
136  pick(Recipient::To);
137 }
138 
139 void RecipientsPicker::slotReplyToClicked()
140 {
141  pick(Recipient::ReplyTo);
142 }
143 
144 void RecipientsPicker::slotCcClicked()
145 {
146  pick(Recipient::Cc);
147 }
148 
149 void RecipientsPicker::slotBccClicked()
150 {
151  pick(Recipient::Bcc);
152 }
153 
154 void RecipientsPicker::slotPicked()
155 {
156  pick(mDefaultType);
157 }
158 
159 void RecipientsPicker::pick(Recipient::Type type)
160 {
161  qCDebug(MESSAGECOMPOSER_LOG) << int(type);
162 
163  const Akonadi::EmailAddressSelection::List selections = mView->emailAddressSelectionWidget()->selectedAddresses();
164 
165  const int count = selections.count();
166  if (count == 0) {
167  return;
168  }
169 
170  if (count > MessageComposerSettings::self()->maximumRecipients()) {
171  KMessageBox::error(this,
172  i18np("You selected 1 recipient. The maximum supported number of "
173  "recipients is %2.\nPlease adapt the selection.",
174  "You selected %1 recipients. The maximum supported number of "
175  "recipients is %2.\nPlease adapt the selection.",
176  count,
177  MessageComposerSettings::self()->maximumRecipients()));
178  return;
179  }
180 
181  bool tooManyAddress = false;
182  for (const Akonadi::EmailAddressSelection &selection : selections) {
183  Recipient recipient;
184  recipient.setType(type);
185  recipient.setEmail(selection.quotedEmail());
186 
187  Q_EMIT pickedRecipient(recipient, tooManyAddress);
188  if (tooManyAddress) {
189  KMessageBox::error(this, i18n("You can not add more recipients."));
190  break;
191  }
192  }
193 }
194 
195 void RecipientsPicker::keyPressEvent(QKeyEvent *event)
196 {
197  if (event->key() == Qt::Key_Escape) {
198  close();
199  }
200 
201  QDialog::keyPressEvent(event);
202 }
203 
204 void RecipientsPicker::readConfig()
205 {
206  create(); // ensure a window is created
207  windowHandle()->resize(QSize(300, 200));
208  KConfigGroup group(KSharedConfig::openStateConfig(), myRecipientsPickerConfigGroupName);
209  KWindowConfig::restoreWindowSize(windowHandle(), group);
210  resize(windowHandle()->size()); // workaround for QTBUG-40584
211 }
212 
213 void RecipientsPicker::writeConfig()
214 {
215  KConfigGroup group(KSharedConfig::openStateConfig(), myRecipientsPickerConfigGroupName);
216  KWindowConfig::saveWindowSize(windowHandle(), group);
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 }
QString readEntry(const char *key, const char *aDefault=nullptr) const
void doubleClicked(const QModelIndex &index)
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
void clicked(bool checked)
void readConfig()
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
The Recipient class.
Definition: recipient.h:27
QAction * create(StandardAction id, const QObject *recvr, const char *slot, QObject *parent)
QString i18n(const char *text, const TYPE &arg...)
void setWindowTitle(const QString &)
QAction * close(const QObject *recvr, const char *slot, QObject *parent)
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
AKONADI_CALENDAR_EXPORT KCalendarCore::Event::Ptr event(const Akonadi::Item &item)
Key_Escape
KSharedConfigPtr config()
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
QString i18np(const char *singular, const char *plural, const TYPE &arg...)
virtual void keyPressEvent(QKeyEvent *e) override
KCONFIGGUI_EXPORT void restoreWindowSize(QWindow *window, const KConfigGroup &config)
void setObjectName(const QString &name)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
int count(const T &value) const const
AddresseeList List
KCONFIGGUI_EXPORT void saveWindowSize(const QWindow *window, KConfigGroup &config, KConfigGroup::WriteConfigFlags options=KConfigGroup::Normal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sat Apr 1 2023 04:01:57 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.