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 // TODO add accountactivitiesabstract
48 setObjectName(QLatin1StringView("RecipientsPicker"));
49 setWindowTitle(i18nc("@title:window", "Select Recipient"));
50
51 auto mainLayout = new QVBoxLayout(this);
52
53 mainLayout->addWidget(mView);
54 mainLayout->setStretchFactor(mView, 1);
55
56 connect(mView->view()->selectionModel(), &QItemSelectionModel::selectionChanged, this, &RecipientsPicker::slotSelectionChanged);
57 connect(mView->view(), &QAbstractItemView::doubleClicked, this, &RecipientsPicker::slotPicked);
58
59 mainLayout->addWidget(mSelectedLabel);
60
61 auto searchLDAPButton = new QPushButton(i18nc("@action:button", "Search &Directory Service"), this);
62 connect(searchLDAPButton, &QPushButton::clicked, this, &RecipientsPicker::slotSearchLDAP);
63 mainLayout->addWidget(searchLDAPButton);
64
65 KConfig config(QStringLiteral("kabldaprc"));
66 KConfigGroup group = config.group(QStringLiteral("LDAP"));
67 int numHosts = group.readEntry("NumSelectedHosts", 0);
68 if (!numHosts) {
69 searchLDAPButton->setVisible(false);
70 }
71
72 auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, this);
73 buttonBox->addButton(mUser1Button, QDialogButtonBox::ActionRole);
74 buttonBox->addButton(mUser2Button, QDialogButtonBox::ActionRole);
75 buttonBox->addButton(mUser3Button, QDialogButtonBox::ActionRole);
76 buttonBox->addButton(mUser4Button, QDialogButtonBox::ActionRole);
77
78 connect(buttonBox, &QDialogButtonBox::rejected, this, &RecipientsPicker::reject);
79 mainLayout->addWidget(buttonBox);
80 mUser4Button->setText(i18n("Add as &Reply-To"));
81 mUser3Button->setText(i18n("Add as &To"));
82 mUser2Button->setText(i18n("Add as CC"));
83 mUser1Button->setText(i18n("Add as &BCC"));
84 connect(mUser1Button, &QPushButton::clicked, this, &RecipientsPicker::slotBccClicked);
85 connect(mUser2Button, &QPushButton::clicked, this, &RecipientsPicker::slotCcClicked);
86 connect(mUser3Button, &QPushButton::clicked, this, &RecipientsPicker::slotToClicked);
87 connect(mUser4Button, &QPushButton::clicked, this, &RecipientsPicker::slotReplyToClicked);
88
89 mView->emailAddressSelectionWidget()->searchLineEdit()->setFocus();
90
91 readConfig();
92
93 slotSelectionChanged();
94}
95
96RecipientsPicker::~RecipientsPicker()
97{
98 writeConfig();
99}
100
101void RecipientsPicker::updateLabel(int nbSelected)
102{
103 if (nbSelected == 0) {
104 mSelectedLabel->setText({});
105 } else {
106 mSelectedLabel->setText(i18np("%1 selected email", "%1 selected emails", nbSelected));
107 }
108}
109
110void RecipientsPicker::slotSelectionChanged()
111{
112 const auto selectedItems{mView->emailAddressSelectionWidget()->selectedAddresses().count()};
113 const bool hasSelection = (selectedItems != 0);
114 mUser1Button->setEnabled(hasSelection);
115 mUser2Button->setEnabled(hasSelection);
116 mUser3Button->setEnabled(hasSelection);
117 mUser4Button->setEnabled(hasSelection);
118 updateLabel(selectedItems);
119}
120
121void RecipientsPicker::setRecipients(const Recipient::List &)
122{
123 mView->view()->selectionModel()->clear();
124}
125
126void RecipientsPicker::setDefaultType(Recipient::Type type)
127{
128 mDefaultType = type;
129 mUser1Button->setDefault(type == Recipient::Bcc);
130 mUser2Button->setDefault(type == Recipient::Cc);
131 mUser3Button->setDefault(type == Recipient::To);
132 mUser4Button->setDefault(type == Recipient::ReplyTo);
133}
134
135void RecipientsPicker::slotToClicked()
136{
137 pick(Recipient::To);
138}
139
140void RecipientsPicker::slotReplyToClicked()
141{
142 pick(Recipient::ReplyTo);
143}
144
145void RecipientsPicker::slotCcClicked()
146{
147 pick(Recipient::Cc);
148}
149
150void RecipientsPicker::slotBccClicked()
151{
152 pick(Recipient::Bcc);
153}
154
155void RecipientsPicker::slotPicked()
156{
157 pick(mDefaultType);
158}
159
160void RecipientsPicker::pick(Recipient::Type type)
161{
162 qCDebug(MESSAGECOMPOSER_LOG) << int(type);
163
164 const Akonadi::EmailAddressSelection::List selections = mView->emailAddressSelectionWidget()->selectedAddresses();
165
166 const int count = selections.count();
167 if (count == 0) {
168 return;
169 }
170
171 if (count > MessageComposerSettings::self()->maximumRecipients()) {
173 i18np("You selected 1 recipient. The maximum supported number of "
174 "recipients is %2.\nPlease adapt the selection.",
175 "You selected %1 recipients. The maximum supported number of "
176 "recipients is %2.\nPlease adapt the selection.",
177 count,
178 MessageComposerSettings::self()->maximumRecipients()));
179 return;
180 }
181
182 bool tooManyAddress = false;
183 for (const Akonadi::EmailAddressSelection &selection : selections) {
184 Recipient recipient;
185 recipient.setType(type);
186 recipient.setEmail(selection.quotedEmail());
187
188 Q_EMIT pickedRecipient(recipient, tooManyAddress);
189 if (tooManyAddress) {
190 KMessageBox::error(this, i18n("You can not add more recipients."));
191 break;
192 }
193 }
194}
195
196void RecipientsPicker::keyPressEvent(QKeyEvent *event)
197{
198 if (event->key() == Qt::Key_Escape) {
199 close();
200 }
201
203}
204
205void RecipientsPicker::readConfig()
206{
207 create(); // ensure a window is created
208 windowHandle()->resize(QSize(300, 200));
209 KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myRecipientsPickerConfigGroupName));
211 resize(windowHandle()->size()); // workaround for QTBUG-40584
212}
213
214void RecipientsPicker::writeConfig()
215{
216 KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myRecipientsPickerConfigGroupName));
218}
219
220void RecipientsPicker::slotSearchLDAP()
221{
222 if (!mLdapSearchDialog) {
223 mLdapSearchDialog = new PimCommon::LdapSearchDialog(this);
224 connect(mLdapSearchDialog, &PimCommon::LdapSearchDialog::contactsAdded, this, &RecipientsPicker::ldapSearchResult);
225 }
226
227 mLdapSearchDialog->setSearchText(mView->emailAddressSelectionWidget()->searchLineEdit()->text());
228 mLdapSearchDialog->show();
229}
230
231void RecipientsPicker::ldapSearchResult()
232{
233 const KContacts::Addressee::List contacts = mLdapSearchDialog->selectedContacts();
234 for (const KContacts::Addressee &contact : contacts) {
235 bool tooManyAddress = false;
236 Q_EMIT pickedRecipient(Recipient(contact.fullEmail(), Recipient::Undefined), tooManyAddress);
237 if (tooManyAddress) {
238 break;
239 }
240 }
241}
242
243#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 &)
qsizetype count() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
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 Mon Nov 4 2024 16:33:25 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.