recipientspicker.cpp 7.57 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*
    Copyright (c) 2010 Volker Krause <vkrause@kde.org>
    This file was part of KMail.
    Copyright (c) 2005 Cornelius Schumacher <schumacher@kde.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "recipientspicker.h"
#include "settings/messagecomposersettings.h"

#include <Akonadi/Contact/EmailAddressSelectionWidget>
Laurent Montel's avatar
Laurent Montel committed
26
#include <Akonadi/Contact/RecipientsPickerWidget>
27
#include <kcontacts/contactgroup.h>
28
#include <PimCommonAkonadi/LdapSearchDialog>
29

30
#include <KConfigGroup>
31
#include <KLocalizedString>
32
#include <KMessageBox>
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <QLineEdit>
#include <QPushButton>
#include "messagecomposer_debug.h"

#include <QKeyEvent>
#include <QTreeView>
#include <QVBoxLayout>
#include <KSharedConfig>
#include <QDialogButtonBox>

using namespace MessageComposer;

RecipientsPicker::RecipientsPicker(QWidget *parent)
Laurent Montel's avatar
Laurent Montel committed
46
    : QDialog(parent)
47
48
{
    setObjectName(QStringLiteral("RecipientsPicker"));
Laurent Montel's avatar
Laurent Montel committed
49
    setWindowTitle(i18nc("@title:window", "Select Recipient"));
50

Laurent Montel's avatar
Laurent Montel committed
51
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
52

Laurent Montel's avatar
Laurent Montel committed
53
    mView = new Akonadi::RecipientsPickerWidget(false, nullptr, this);
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
    mainLayout->addWidget(mView);
    mainLayout->setStretchFactor(mView, 1);

    connect(mView->view()->selectionModel(), &QItemSelectionModel::selectionChanged,
            this, &RecipientsPicker::slotSelectionChanged);
    connect(mView->view(), &QAbstractItemView::doubleClicked,
            this, &RecipientsPicker::slotPicked);

    QPushButton *searchLDAPButton = new QPushButton(i18n("Search &Directory Service"), this);
    connect(searchLDAPButton, &QPushButton::clicked, this, &RecipientsPicker::slotSearchLDAP);
    mainLayout->addWidget(searchLDAPButton);

    KConfig config(QStringLiteral("kabldaprc"));
    KConfigGroup group = config.group("LDAP");
    int numHosts = group.readEntry("NumSelectedHosts", 0);
    if (!numHosts) {
        searchLDAPButton->setVisible(false);
    }

Laurent Montel's avatar
Laurent Montel committed
73
    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, this);
74
75
76
77
78
79
    mUser1Button = new QPushButton;
    buttonBox->addButton(mUser1Button, QDialogButtonBox::ActionRole);
    mUser2Button = new QPushButton;
    buttonBox->addButton(mUser2Button, QDialogButtonBox::ActionRole);
    mUser3Button = new QPushButton;
    buttonBox->addButton(mUser3Button, QDialogButtonBox::ActionRole);
80
81
82
    mUser4Button = new QPushButton;
    buttonBox->addButton(mUser4Button, QDialogButtonBox::ActionRole);

83
84
    connect(buttonBox, &QDialogButtonBox::rejected, this, &RecipientsPicker::reject);
    mainLayout->addWidget(buttonBox);
85
    mUser4Button->setText(i18n("Add as &Reply-To"));
86
87
88
89
90
91
    mUser3Button->setText(i18n("Add as &To"));
    mUser2Button->setText(i18n("Add as CC"));
    mUser1Button->setText(i18n("Add as &BCC"));
    connect(mUser1Button, &QPushButton::clicked, this, &RecipientsPicker::slotBccClicked);
    connect(mUser2Button, &QPushButton::clicked, this, &RecipientsPicker::slotCcClicked);
    connect(mUser3Button, &QPushButton::clicked, this, &RecipientsPicker::slotToClicked);
92
    connect(mUser4Button, &QPushButton::clicked, this, &RecipientsPicker::slotReplyToClicked);
93

Laurent Montel's avatar
Laurent Montel committed
94
    mView->emailAddressSelectionWidget()->searchLineEdit()->setFocus();
95
96
97
98
99
100
101
102
103
104
105
106
107

    readConfig();

    slotSelectionChanged();
}

RecipientsPicker::~RecipientsPicker()
{
    writeConfig();
}

void RecipientsPicker::slotSelectionChanged()
{
Laurent Montel's avatar
Laurent Montel committed
108
    const bool hasSelection = !mView->emailAddressSelectionWidget()->selectedAddresses().isEmpty();
109
110
111
    mUser1Button->setEnabled(hasSelection);
    mUser2Button->setEnabled(hasSelection);
    mUser3Button->setEnabled(hasSelection);
112
    mUser4Button->setEnabled(hasSelection);
113
114
115
116
117
118
119
120
121
122
123
124
125
}

void RecipientsPicker::setRecipients(const Recipient::List &)
{
    mView->view()->selectionModel()->clear();
}

void RecipientsPicker::setDefaultType(Recipient::Type type)
{
    mDefaultType = type;
    mUser1Button->setDefault(type == Recipient::To);
    mUser2Button->setDefault(type == Recipient::Cc);
    mUser3Button->setDefault(type == Recipient::Bcc);
126
    mUser4Button->setDefault(type == Recipient::ReplyTo);
127
128
129
130
131
132
133
}

void RecipientsPicker::slotToClicked()
{
    pick(Recipient::To);
}

134
135
136
137
138
void RecipientsPicker::slotReplyToClicked()
{
    pick(Recipient::ReplyTo);
}

139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
void RecipientsPicker::slotCcClicked()
{
    pick(Recipient::Cc);
}

void RecipientsPicker::slotBccClicked()
{
    pick(Recipient::Bcc);
}

void RecipientsPicker::slotPicked()
{
    pick(mDefaultType);
}

void RecipientsPicker::pick(Recipient::Type type)
{
    qCDebug(MESSAGECOMPOSER_LOG) << int(type);

Laurent Montel's avatar
Laurent Montel committed
158
    const Akonadi::EmailAddressSelection::List selections = mView->emailAddressSelectionWidget()->selectedAddresses();
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175

    const int count = selections.count();
    if (count == 0) {
        return;
    }

    if (count > MessageComposerSettings::self()->maximumRecipients()) {
        KMessageBox::sorry(this,
                           i18np("You selected 1 recipient. The maximum supported number of "
                                 "recipients is %2. Please adapt the selection.",
                                 "You selected %1 recipients. The maximum supported number of "
                                 "recipients is %2. Please adapt the selection.", count,
                                 MessageComposerSettings::self()->maximumRecipients()));
        return;
    }

    bool tooManyAddress = false;
Laurent Montel's avatar
Laurent Montel committed
176
    for (const Akonadi::EmailAddressSelection &selection : selections) {
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
        Recipient recipient;
        recipient.setType(type);
        recipient.setEmail(selection.quotedEmail());

        Q_EMIT pickedRecipient(recipient, tooManyAddress);
        if (tooManyAddress) {
            break;
        }
    }
}

void RecipientsPicker::keyPressEvent(QKeyEvent *event)
{
    if (event->key() == Qt::Key_Escape) {
        close();
    }

    QDialog::keyPressEvent(event);
}

void RecipientsPicker::readConfig()
{
    KSharedConfig::Ptr cfg = KSharedConfig::openConfig();
    KConfigGroup group(cfg, "RecipientsPicker");
    QSize size = group.readEntry("Size", QSize());
    if (!size.isEmpty()) {
        resize(size);
    }
}

void RecipientsPicker::writeConfig()
{
    KSharedConfig::Ptr cfg = KSharedConfig::openConfig();
    KConfigGroup group(cfg, "RecipientsPicker");
    group.writeEntry("Size", size());
}

void RecipientsPicker::slotSearchLDAP()
{
    if (!mLdapSearchDialog) {
217
218
        mLdapSearchDialog = new PimCommon::LdapSearchDialog(this);
        connect(mLdapSearchDialog, &PimCommon::LdapSearchDialog::contactsAdded, this, &RecipientsPicker::ldapSearchResult);
219
220
    }

Laurent Montel's avatar
Laurent Montel committed
221
    mLdapSearchDialog->setSearchText(mView->emailAddressSelectionWidget()->searchLineEdit()->text());
222
223
224
225
226
227
    mLdapSearchDialog->show();
}

void RecipientsPicker::ldapSearchResult()
{
    const KContacts::Addressee::List contacts = mLdapSearchDialog->selectedContacts();
228
    for (const KContacts::Addressee &contact : contacts) {
229
230
231
232
233
234
235
        bool tooManyAddress = false;
        Q_EMIT pickedRecipient(Recipient(contact.fullEmail(), Recipient::Undefined), tooManyAddress);
        if (tooManyAddress) {
            break;
        }
    }
}