Messagelib

recipientspicker.cpp
1/*
2 SPDX-FileCopyrightText: 2010 Volker Krause <vkrause@kde.org>
3 This file was part of KMail.
4 SPDX-FileCopyrightText: 2005 Cornelius Schumacher <schumacher@kde.org>
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
33using namespace MessageComposer;
34namespace
35{
36static const char myRecipientsPickerConfigGroupName[] = "RecipientsPicker";
37}
38RecipientsPicker::RecipientsPicker(QWidget *parent)
39 : QDialog(parent)
40 , mView(new Akonadi::RecipientsPickerWidget(true, nullptr, this))
41 , mUser1Button(new QPushButton(this))
42 , mUser2Button(new QPushButton(this))
43 , mUser3Button(new QPushButton(this))
44 , mUser4Button(new QPushButton(this))
45 , mSelectedLabel(new QLabel(this))
46{
47 setObjectName(QLatin1StringView("RecipientsPicker"));
48 setWindowTitle(i18nc("@title:window", "Select Recipient"));
49
50 auto mainLayout = new QVBoxLayout(this);
51
52 mainLayout->addWidget(mView);
53 mainLayout->setStretchFactor(mView, 1);
54
55 connect(mView->view()->selectionModel(), &QItemSelectionModel::selectionChanged, this, &RecipientsPicker::slotSelectionChanged);
56 connect(mView->view(), &QAbstractItemView::doubleClicked, this, &RecipientsPicker::slotPicked);
57
58 mainLayout->addWidget(mSelectedLabel);
59
60 auto searchLDAPButton = new QPushButton(i18n("Search &Directory Service"), this);
61 connect(searchLDAPButton, &QPushButton::clicked, this, &RecipientsPicker::slotSearchLDAP);
62 mainLayout->addWidget(searchLDAPButton);
63
64 KConfig config(QStringLiteral("kabldaprc"));
65 KConfigGroup group = config.group(QStringLiteral("LDAP"));
66 int numHosts = group.readEntry("NumSelectedHosts", 0);
67 if (!numHosts) {
68 searchLDAPButton->setVisible(false);
69 }
70
71 auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, this);
72 buttonBox->addButton(mUser1Button, QDialogButtonBox::ActionRole);
73 buttonBox->addButton(mUser2Button, QDialogButtonBox::ActionRole);
74 buttonBox->addButton(mUser3Button, QDialogButtonBox::ActionRole);
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
95RecipientsPicker::~RecipientsPicker()
96{
97 writeConfig();
98}
99
100void 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
109void 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
120void RecipientsPicker::setRecipients(const Recipient::List &)
121{
122 mView->view()->selectionModel()->clear();
123}
124
125void 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
134void RecipientsPicker::slotToClicked()
135{
136 pick(Recipient::To);
137}
138
139void RecipientsPicker::slotReplyToClicked()
140{
141 pick(Recipient::ReplyTo);
142}
143
144void RecipientsPicker::slotCcClicked()
145{
146 pick(Recipient::Cc);
147}
148
149void RecipientsPicker::slotBccClicked()
150{
151 pick(Recipient::Bcc);
152}
153
154void RecipientsPicker::slotPicked()
155{
156 pick(mDefaultType);
157}
158
159void 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()) {
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
195void RecipientsPicker::keyPressEvent(QKeyEvent *event)
196{
197 if (event->key() == Qt::Key_Escape) {
198 close();
199 }
200
202}
203
204void RecipientsPicker::readConfig()
205{
206 create(); // ensure a window is created
207 windowHandle()->resize(QSize(300, 200));
210 resize(windowHandle()->size()); // workaround for QTBUG-40584
211}
212
213void RecipientsPicker::writeConfig()
214{
217}
218
219void 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
230void 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}
241
242#include "moc_recipientspicker.cpp"
KConfigGroup group(const QString &group)
QString readEntry(const char *key, const char *aDefault=nullptr) const
AddresseeList List
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
The Recipient class.
Definition recipient.h:28
KContacts::Addressee::List selectedContacts() const
void setSearchText(const QString &text)
QString i18np(const char *singular, const char *plural, const TYPE &arg...)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
KCONFIGGUI_EXPORT void saveWindowSize(const QWindow *window, KConfigGroup &config, KConfigGroup::WriteConfigFlags options=KConfigGroup::Normal)
KCONFIGGUI_EXPORT void restoreWindowSize(QWindow *window, const KConfigGroup &config)
Simple interface that both EncryptJob and SignEncryptJob implement so the composer can extract some e...
void clicked(bool checked)
void doubleClicked(const QModelIndex &index)
virtual void keyPressEvent(QKeyEvent *e) override
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
void setText(const QString &)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T qobject_cast(QObject *object)
void setDefault(bool)
Key_Escape
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
bool close()
void create(WId window, bool initializeWindow, bool destroyOldWindow)
void setEnabled(bool)
virtual bool event(QEvent *event) override
void show()
void resize(const QSize &)
QWindow * windowHandle() const const
void resize(const QSize &newSize)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 24 2024 11:55:43 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.