contactselectionwidget.cpp 10.9 KB
Newer Older
1
/*
Allen Winter's avatar
Allen Winter committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  Copyright (c) 2009 Tobias Koenig <tokoe@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.
18
19
20
21
*/

#include "contactselectionwidget.h"

Laurent Montel's avatar
Port++    
Laurent Montel committed
22
23
24
25
26
#include <AkonadiWidgets/CollectionComboBox>
#include <AkonadiCore/EntityTreeModel>
#include <AkonadiCore/ItemFetchJob>
#include <AkonadiCore/ItemFetchScope>
#include <AkonadiCore/RecursiveItemFetchJob>
Allen Winter's avatar
Allen Winter committed
27

Laurent Montel's avatar
Laurent Montel committed
28
#include <KLocalizedString>
29

30
31
32
33
34
35
36
37
#include <QButtonGroup>
#include <QCheckBox>
#include <QGridLayout>
#include <QGroupBox>
#include <QItemSelectionModel>
#include <QLabel>
#include <QRadioButton>
#include <QVBoxLayout>
38

Laurent Montel's avatar
Laurent Montel committed
39
ContactSelectionWidget::ContactSelectionWidget(QItemSelectionModel *selectionModel,
Laurent Montel's avatar
Laurent Montel committed
40
        QWidget *parent)
41
    : QWidget(parent), mSelectionModel(selectionModel), mAddContactGroup(false)
42
{
Laurent Montel's avatar
Laurent Montel committed
43
44
    initGui();

Laurent Montel's avatar
Laurent Montel committed
45
46
47
    mSelectedContactsButton->setEnabled(mSelectionModel->hasSelection());
    mAddressBookSelection->setEnabled(false);
    mAddressBookSelectionRecursive->setEnabled(false);
Laurent Montel's avatar
Laurent Montel committed
48

Laurent Montel's avatar
Laurent Montel committed
49
50
    connect(mAddressBookContactsButton, &QRadioButton::toggled, mAddressBookSelection, &Akonadi::CollectionComboBox::setEnabled);
    connect(mAddressBookContactsButton, &QRadioButton::toggled, mAddressBookSelectionRecursive, &QCheckBox::setEnabled);
Laurent Montel's avatar
Laurent Montel committed
51
52

    // apply default configuration
Laurent Montel's avatar
Laurent Montel committed
53
54
    if (mSelectionModel->hasSelection()) {
        mSelectedContactsButton->setChecked(true);
Laurent Montel's avatar
Laurent Montel committed
55
    } else {
Laurent Montel's avatar
Laurent Montel committed
56
        mAllContactsButton->setChecked(true);
Laurent Montel's avatar
Laurent Montel committed
57
    }
58
59
}

Laurent Montel's avatar
Laurent Montel committed
60
void ContactSelectionWidget::setMessageText(const QString &message)
61
{
62
63
64
65
    if (!message.isEmpty()) {
        mMessageLabel->setText(message);
        mMessageLabel->show();
    }
66
67
}

Laurent Montel's avatar
Laurent Montel committed
68
void ContactSelectionWidget::setDefaultAddressBook(const Akonadi::Collection &addressBook)
69
{
Laurent Montel's avatar
Laurent Montel committed
70
    mAddressBookSelection->setDefaultCollection(addressBook);
71
72
}

Laurent Montel's avatar
Laurent Montel committed
73
74
75
76
77
78
79
80
81
82
83
84
85
Akonadi::Item::List ContactSelectionWidget::selectedItems() const
{
    if (mAllContactsButton->isChecked()) {
        return collectAllItems();
    } else if (mSelectedContactsButton->isChecked()) {
        return collectSelectedItems();
    } else if (mAddressBookContactsButton->isChecked()) {
        return collectAddressBookItems();
    }

    return Akonadi::Item::List();
}

Laurent Montel's avatar
Laurent Montel committed
86
ContactList ContactSelectionWidget::selectedContacts() const
87
{
Laurent Montel's avatar
Laurent Montel committed
88
    if (mAllContactsButton->isChecked()) {
Laurent Montel's avatar
Laurent Montel committed
89
        return collectAllContacts();
Laurent Montel's avatar
Laurent Montel committed
90
    } else if (mSelectedContactsButton->isChecked()) {
Laurent Montel's avatar
Laurent Montel committed
91
        return collectSelectedContacts();
Laurent Montel's avatar
Laurent Montel committed
92
    } else if (mAddressBookContactsButton->isChecked()) {
Laurent Montel's avatar
Laurent Montel committed
93
94
95
        return collectAddressBookContacts();
    }

Laurent Montel's avatar
Laurent Montel committed
96
    return ContactList();
97
98
}

Laurent Montel's avatar
Laurent Montel committed
99
100
101
102
103
void ContactSelectionWidget::setAddGroupContact(bool addGroupContact)
{
    mAddContactGroup = addGroupContact;
}

104
105
void ContactSelectionWidget::initGui()
{
Laurent Montel's avatar
Laurent Montel committed
106
    QVBoxLayout *layout = new QVBoxLayout(this);
Laurent Montel's avatar
Laurent Montel committed
107
    layout->setMargin(0);
Laurent Montel's avatar
Laurent Montel committed
108
109

    mMessageLabel = new QLabel;
Laurent Montel's avatar
Laurent Montel committed
110
    layout->addWidget(mMessageLabel);
111
    mMessageLabel->hide();
Laurent Montel's avatar
Laurent Montel committed
112

Laurent Montel's avatar
Laurent Montel committed
113
    QButtonGroup *group = new QButtonGroup(this);
Laurent Montel's avatar
Laurent Montel committed
114
115
116
117

    QGroupBox *groupBox = new QGroupBox;

    QGridLayout *boxLayout = new QGridLayout;
Laurent Montel's avatar
Laurent Montel committed
118
    groupBox->setLayout(boxLayout);
Laurent Montel's avatar
Laurent Montel committed
119

Laurent Montel's avatar
Laurent Montel committed
120
    mAllContactsButton = new QRadioButton(i18nc("@option:radio", "All contacts"));
Laurent Montel's avatar
Laurent Montel committed
121
    mAllContactsButton->setToolTip(
Laurent Montel's avatar
Laurent Montel committed
122
        i18nc("@info:tooltip", "All contacts from all your address books"));
Laurent Montel's avatar
Laurent Montel committed
123
    mAllContactsButton->setWhatsThis(
Laurent Montel's avatar
Laurent Montel committed
124
125
126
        i18nc("@info:whatsthis",
              "Choose this option you want to select all your contacts from "
              "all your address books."));
Laurent Montel's avatar
Laurent Montel committed
127

Laurent Montel's avatar
Laurent Montel committed
128
    mSelectedContactsButton = new QRadioButton(i18nc("@option:radio", "Selected contacts"));
Laurent Montel's avatar
Laurent Montel committed
129
    mSelectedContactsButton->setToolTip(
Laurent Montel's avatar
Laurent Montel committed
130
        i18nc("@info:tooltip", "Only the contacts currently selected"));
Laurent Montel's avatar
Laurent Montel committed
131
    mSelectedContactsButton->setWhatsThis(
Laurent Montel's avatar
Laurent Montel committed
132
133
134
        i18nc("@info:whatsthis",
              "Choose this option if you want only the contacts you have already "
              "selected in the graphical interface."));
Laurent Montel's avatar
Laurent Montel committed
135

Laurent Montel's avatar
Laurent Montel committed
136
    mAddressBookContactsButton = new QRadioButton(i18nc("@option:radio", "All contacts from:"));
Laurent Montel's avatar
Laurent Montel committed
137
    mAddressBookContactsButton->setToolTip(
Laurent Montel's avatar
Laurent Montel committed
138
        i18nc("@info:tooltip", "All contacts from a chosen address book"));
Laurent Montel's avatar
Laurent Montel committed
139
    mAddressBookContactsButton->setWhatsThis(
Laurent Montel's avatar
Laurent Montel committed
140
141
142
143
144
        i18nc("@info:whatsthis",
              "Choose this option if you want to select all the contacts from only one "
              "of your address books.  Once this option is clicked you will be provided "
              "a drop down box listing all those address books and permitted to select "
              "the one you want."));
Laurent Montel's avatar
Laurent Montel committed
145
146

    mAddressBookSelection = new Akonadi::CollectionComboBox;
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
147
    mAddressBookSelection->setMimeTypeFilter(QStringList() << KContacts::Addressee::mimeType());
Laurent Montel's avatar
Laurent Montel committed
148
149
150
    mAddressBookSelection->setAccessRightsFilter(Akonadi::Collection::ReadOnly);
    mAddressBookSelection->setExcludeVirtualCollections(true);
    mAddressBookSelectionRecursive = new QCheckBox(i18nc("@option:check", "Include Subfolders"));
Laurent Montel's avatar
Laurent Montel committed
151
    mAddressBookSelectionRecursive->setToolTip(
Laurent Montel's avatar
Laurent Montel committed
152
        i18nc("@info:tooltip", "Select all subfolders including the top-level folder"));
Laurent Montel's avatar
Laurent Montel committed
153
    mAddressBookSelectionRecursive->setWhatsThis(
Laurent Montel's avatar
Laurent Montel committed
154
155
156
157
        i18nc("@info:whatsthis",
              "Check this box if you want to select all contacts from this folder, "
              "including all subfolders.  If you only want the contacts from the "
              "top-level folder then leave this box unchecked."));
Laurent Montel's avatar
Laurent Montel committed
158

Laurent Montel's avatar
Laurent Montel committed
159
160
161
    group->addButton(mAllContactsButton);
    group->addButton(mSelectedContactsButton);
    group->addButton(mAddressBookContactsButton);
Laurent Montel's avatar
Laurent Montel committed
162

Laurent Montel's avatar
Laurent Montel committed
163
164
165
    boxLayout->addWidget(mAllContactsButton, 0, 0, 1, 2);
    boxLayout->addWidget(mSelectedContactsButton, 1, 0, 1, 2);
    boxLayout->addWidget(mAddressBookContactsButton, 2, 0, Qt::AlignTop);
Laurent Montel's avatar
Laurent Montel committed
166
167

    QVBoxLayout *addressBookLayout = new QVBoxLayout;
Laurent Montel's avatar
Laurent Montel committed
168
169
170
    addressBookLayout->setMargin(0);
    addressBookLayout->addWidget(mAddressBookSelection);
    addressBookLayout->addWidget(mAddressBookSelectionRecursive);
Laurent Montel's avatar
Laurent Montel committed
171

Laurent Montel's avatar
Laurent Montel committed
172
    boxLayout->addLayout(addressBookLayout, 2, 1);
Laurent Montel's avatar
Laurent Montel committed
173

Laurent Montel's avatar
Laurent Montel committed
174
175
    layout->addWidget(groupBox);
    layout->addStretch(1);
176
177
}

Laurent Montel's avatar
Laurent Montel committed
178
179
180
181
182
183
184
185
186
187
188
189
190
191
Akonadi::Item::List ContactSelectionWidget::collectAllItems() const
{
    Akonadi::RecursiveItemFetchJob *job =
        new Akonadi::RecursiveItemFetchJob(Akonadi::Collection::root(),
                                           QStringList() << KContacts::Addressee::mimeType());
    job->fetchScope().fetchFullPayload();

    if (!job->exec()) {
        return Akonadi::Item::List();
    }

    return job->items();
}

Laurent Montel's avatar
Laurent Montel committed
192
ContactList ContactSelectionWidget::collectAllContacts() const
193
{
Laurent Montel's avatar
Laurent Montel committed
194
    ContactList contacts;
Laurent Montel's avatar
Laurent Montel committed
195
    Akonadi::RecursiveItemFetchJob *job =
Laurent Montel's avatar
Laurent Montel committed
196
        new Akonadi::RecursiveItemFetchJob(Akonadi::Collection::root(),
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
197
                                           QStringList() << KContacts::Addressee::mimeType());
Laurent Montel's avatar
Laurent Montel committed
198
    job->fetchScope().fetchFullPayload();
199

Laurent Montel's avatar
Laurent Montel committed
200
    if (!job->exec()) {
Laurent Montel's avatar
Laurent Montel committed
201
202
        return contacts;
    }
203

Laurent Montel's avatar
Laurent Montel committed
204
205
206
207
    foreach (const Akonadi::Item &item, job->items()) {
        if (item.isValid()) {
            if (item.hasPayload<KContacts::Addressee>()) {
                contacts.append(item.payload<KContacts::Addressee>());
Laurent Montel's avatar
Minor    
Laurent Montel committed
208
            }
Laurent Montel's avatar
Laurent Montel committed
209
        }
Allen Winter's avatar
Allen Winter committed
210
    }
211

Laurent Montel's avatar
Laurent Montel committed
212
    return contacts;
213
214
}

Laurent Montel's avatar
Laurent Montel committed
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
Akonadi::Item::List ContactSelectionWidget::collectSelectedItems() const
{
    Akonadi::Item::List items;

    const QModelIndexList indexes = mSelectionModel->selectedRows(0);
    for (int i = 0; i < indexes.count(); ++i) {
        const QModelIndex index = indexes.at(i);
        if (index.isValid()) {
            const Akonadi::Item item =
                index.data(Akonadi::EntityTreeModel::ItemRole).value<Akonadi::Item>();
            if (item.isValid()) {
                items.append(item);
            }
        }
    }

    return items;
}

Laurent Montel's avatar
Laurent Montel committed
234
ContactList ContactSelectionWidget::collectSelectedContacts() const
235
{
Laurent Montel's avatar
Laurent Montel committed
236
    ContactList contacts;
Laurent Montel's avatar
Laurent Montel committed
237

Laurent Montel's avatar
Laurent Montel committed
238
239
240
241
    const QModelIndexList indexes = mSelectionModel->selectedRows(0);
    for (int i = 0; i < indexes.count(); ++i) {
        const QModelIndex index = indexes.at(i);
        if (index.isValid()) {
Laurent Montel's avatar
Laurent Montel committed
242
            const Akonadi::Item item =
Laurent Montel's avatar
Laurent Montel committed
243
244
245
246
                index.data(Akonadi::EntityTreeModel::ItemRole).value<Akonadi::Item>();
            if (item.isValid()) {
                if (item.hasPayload<KContacts::Addressee>()) {
                    contacts.append(item.payload<KContacts::Addressee>());
Laurent Montel's avatar
Minor    
Laurent Montel committed
247
                }
Laurent Montel's avatar
Laurent Montel committed
248
249
            }
        }
250
251
    }

Laurent Montel's avatar
Laurent Montel committed
252
    return contacts;
253
254
}

Laurent Montel's avatar
Laurent Montel committed
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
Akonadi::Item::List ContactSelectionWidget::collectAddressBookItems() const
{
    Akonadi::Item::List items;

    const Akonadi::Collection collection = mAddressBookSelection->currentCollection();
    if (!collection.isValid()) {
        return items;
    }

    if (mAddressBookSelectionRecursive->isChecked()) {
        Akonadi::RecursiveItemFetchJob *job =
            new Akonadi::RecursiveItemFetchJob(collection,
                                               QStringList() << KContacts::Addressee::mimeType());
        job->fetchScope().fetchFullPayload();

        if (!job->exec()) {
            return items;
        }

        items = job->items();
    } else {
        Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob(collection);
        job->fetchScope().fetchFullPayload();

        if (!job->exec()) {
            return items;
        }
        items = job->items();
    }

    return items;

}

Laurent Montel's avatar
Laurent Montel committed
289
ContactList ContactSelectionWidget::collectAddressBookContacts() const
290
{
Laurent Montel's avatar
Laurent Montel committed
291
    ContactList contacts;
292

Laurent Montel's avatar
Laurent Montel committed
293
    const Akonadi::Collection collection = mAddressBookSelection->currentCollection();
Laurent Montel's avatar
Laurent Montel committed
294
    if (!collection.isValid()) {
Laurent Montel's avatar
Laurent Montel committed
295
        return contacts;
Allen Winter's avatar
Allen Winter committed
296
    }
297

Laurent Montel's avatar
Laurent Montel committed
298
    if (mAddressBookSelectionRecursive->isChecked()) {
Laurent Montel's avatar
Laurent Montel committed
299
        Akonadi::RecursiveItemFetchJob *job =
Laurent Montel's avatar
Laurent Montel committed
300
            new Akonadi::RecursiveItemFetchJob(collection,
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
301
                                               QStringList() << KContacts::Addressee::mimeType());
Laurent Montel's avatar
Laurent Montel committed
302
303
        job->fetchScope().fetchFullPayload();

Laurent Montel's avatar
Laurent Montel committed
304
        if (!job->exec()) {
Laurent Montel's avatar
Laurent Montel committed
305
306
307
            return contacts;
        }

Laurent Montel's avatar
Laurent Montel committed
308
        foreach (const Akonadi::Item &item, job->items()) {
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
309
            if (item.hasPayload<KContacts::Addressee>()) {
310
                contacts.append(item.payload<KContacts::Addressee>());
Laurent Montel's avatar
Laurent Montel committed
311
312
313
            }
        }
    } else {
Laurent Montel's avatar
Laurent Montel committed
314
        Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob(collection);
Laurent Montel's avatar
Laurent Montel committed
315
316
        job->fetchScope().fetchFullPayload();

Laurent Montel's avatar
Laurent Montel committed
317
        if (!job->exec()) {
Laurent Montel's avatar
Laurent Montel committed
318
319
320
            return contacts;
        }

Laurent Montel's avatar
Laurent Montel committed
321
        foreach (const Akonadi::Item &item, job->items()) {
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
322
            if (item.hasPayload<KContacts::Addressee>()) {
323
                contacts.append(item.payload<KContacts::Addressee>());
Laurent Montel's avatar
Laurent Montel committed
324
325
            }
        }
326
327
    }

Laurent Montel's avatar
Laurent Montel committed
328
    return contacts;
329
330
}