Remove double margin
[kdepim.git] / kaddressbook / contactselectionwidget.cpp
blob5ffec812c89b820d11cac97f6c567225983a8cc6
1 /*
2 Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
20 #include "contactselectionwidget.h"
21 #include "utils.h"
23 #include <AkonadiWidgets/CollectionComboBox>
24 #include <AkonadiCore/EntityTreeModel>
25 #include <AkonadiCore/ItemFetchJob>
26 #include <AkonadiCore/ItemFetchScope>
27 #include <AkonadiCore/RecursiveItemFetchJob>
29 #include <KLocalizedString>
31 #include <QButtonGroup>
32 #include <QCheckBox>
33 #include <QGridLayout>
34 #include <QGroupBox>
35 #include <QItemSelectionModel>
36 #include <QLabel>
37 #include <QRadioButton>
38 #include <QVBoxLayout>
40 ContactSelectionWidget::ContactSelectionWidget(QItemSelectionModel *selectionModel,
41 QWidget *parent)
42 : QWidget(parent), mSelectionModel(selectionModel), mAddContactGroup(false)
44 initGui();
46 mSelectedContactsButton->setEnabled(mSelectionModel->hasSelection());
47 mAddressBookSelection->setEnabled(false);
48 mAddressBookSelectionRecursive->setEnabled(false);
50 connect(mAddressBookContactsButton, &QRadioButton::toggled, mAddressBookSelection, &Akonadi::CollectionComboBox::setEnabled);
51 connect(mAddressBookContactsButton, &QRadioButton::toggled, mAddressBookSelectionRecursive, &QCheckBox::setEnabled);
53 // apply default configuration
54 if (mSelectionModel->hasSelection()) {
55 mSelectedContactsButton->setChecked(true);
56 } else {
57 mAllContactsButton->setChecked(true);
61 void ContactSelectionWidget::setMessageText(const QString &message)
63 mMessageLabel->setText(message);
66 void ContactSelectionWidget::setDefaultAddressBook(const Akonadi::Collection &addressBook)
68 mAddressBookSelection->setDefaultCollection(addressBook);
71 ContactList ContactSelectionWidget::selectedContacts() const
73 if (mAllContactsButton->isChecked()) {
74 return collectAllContacts();
75 } else if (mSelectedContactsButton->isChecked()) {
76 return collectSelectedContacts();
77 } else if (mAddressBookContactsButton->isChecked()) {
78 return collectAddressBookContacts();
81 return ContactList();
84 void ContactSelectionWidget::setAddGroupContact(bool addGroupContact)
86 mAddContactGroup = addGroupContact;
89 void ContactSelectionWidget::initGui()
91 QVBoxLayout *layout = new QVBoxLayout(this);
93 mMessageLabel = new QLabel;
94 layout->addWidget(mMessageLabel);
96 QButtonGroup *group = new QButtonGroup(this);
98 QGroupBox *groupBox = new QGroupBox;
100 QGridLayout *boxLayout = new QGridLayout;
101 groupBox->setLayout(boxLayout);
103 mAllContactsButton = new QRadioButton(i18nc("@option:radio", "All contacts"));
104 mAllContactsButton->setToolTip(
105 i18nc("@info:tooltip", "All contacts from all your address books"));
106 mAllContactsButton->setWhatsThis(
107 i18nc("@info:whatsthis",
108 "Choose this option you want to select all your contacts from "
109 "all your address books."));
111 mSelectedContactsButton = new QRadioButton(i18nc("@option:radio", "Selected contacts"));
112 mSelectedContactsButton->setToolTip(
113 i18nc("@info:tooltip", "Only the contacts currently selected"));
114 mSelectedContactsButton->setWhatsThis(
115 i18nc("@info:whatsthis",
116 "Choose this option if you want only the contacts you have already "
117 "selected in the graphical interface."));
119 mAddressBookContactsButton = new QRadioButton(i18nc("@option:radio", "All contacts from:"));
120 mAddressBookContactsButton->setToolTip(
121 i18nc("@info:tooltip", "All contacts from a chosen address book"));
122 mAddressBookContactsButton->setWhatsThis(
123 i18nc("@info:whatsthis",
124 "Choose this option if you want to select all the contacts from only one "
125 "of your address books. Once this option is clicked you will be provided "
126 "a drop down box listing all those address books and permitted to select "
127 "the one you want."));
129 mAddressBookSelection = new Akonadi::CollectionComboBox;
130 mAddressBookSelection->setMimeTypeFilter(QStringList() << KContacts::Addressee::mimeType());
131 mAddressBookSelection->setAccessRightsFilter(Akonadi::Collection::ReadOnly);
132 mAddressBookSelection->setExcludeVirtualCollections(true);
133 mAddressBookSelectionRecursive = new QCheckBox(i18nc("@option:check", "Include Subfolders"));
134 mAddressBookSelectionRecursive->setToolTip(
135 i18nc("@info:tooltip", "Select all subfolders including the top-level folder"));
136 mAddressBookSelectionRecursive->setWhatsThis(
137 i18nc("@info:whatsthis",
138 "Check this box if you want to select all contacts from this folder, "
139 "including all subfolders. If you only want the contacts from the "
140 "top-level folder then leave this box unchecked."));
142 group->addButton(mAllContactsButton);
143 group->addButton(mSelectedContactsButton);
144 group->addButton(mAddressBookContactsButton);
146 boxLayout->addWidget(mAllContactsButton, 0, 0, 1, 2);
147 boxLayout->addWidget(mSelectedContactsButton, 1, 0, 1, 2);
148 boxLayout->addWidget(mAddressBookContactsButton, 2, 0, Qt::AlignTop);
150 QVBoxLayout *addressBookLayout = new QVBoxLayout;
151 addressBookLayout->setMargin(0);
152 addressBookLayout->addWidget(mAddressBookSelection);
153 addressBookLayout->addWidget(mAddressBookSelectionRecursive);
155 boxLayout->addLayout(addressBookLayout, 2, 1);
157 layout->addWidget(groupBox);
158 layout->addStretch(1);
161 ContactList ContactSelectionWidget::collectAllContacts() const
163 ContactList contacts;
164 Akonadi::RecursiveItemFetchJob *job =
165 new Akonadi::RecursiveItemFetchJob(Akonadi::Collection::root(),
166 QStringList() << KContacts::Addressee::mimeType());
167 job->fetchScope().fetchFullPayload();
169 if (!job->exec()) {
170 return contacts;
173 foreach (const Akonadi::Item &item, job->items()) {
174 if (item.isValid()) {
175 if (item.hasPayload<KContacts::Addressee>()) {
176 contacts.append(item.payload<KContacts::Addressee>());
181 return contacts;
184 ContactList ContactSelectionWidget::collectSelectedContacts() const
186 ContactList contacts;
188 const QModelIndexList indexes = mSelectionModel->selectedRows(0);
189 for (int i = 0; i < indexes.count(); ++i) {
190 const QModelIndex index = indexes.at(i);
191 if (index.isValid()) {
192 const Akonadi::Item item =
193 index.data(Akonadi::EntityTreeModel::ItemRole).value<Akonadi::Item>();
194 if (item.isValid()) {
195 if (item.hasPayload<KContacts::Addressee>()) {
196 contacts.append(item.payload<KContacts::Addressee>());
202 return contacts;
205 ContactList ContactSelectionWidget::collectAddressBookContacts() const
207 ContactList contacts;
209 const Akonadi::Collection collection = mAddressBookSelection->currentCollection();
210 if (!collection.isValid()) {
211 return contacts;
214 if (mAddressBookSelectionRecursive->isChecked()) {
215 Akonadi::RecursiveItemFetchJob *job =
216 new Akonadi::RecursiveItemFetchJob(collection,
217 QStringList() << KContacts::Addressee::mimeType());
218 job->fetchScope().fetchFullPayload();
220 if (!job->exec()) {
221 return contacts;
224 foreach (const Akonadi::Item &item, job->items()) {
225 if (item.hasPayload<KContacts::Addressee>()) {
226 contacts.append(item.payload<KContacts::Addressee>());
229 } else {
230 Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob(collection);
231 job->fetchScope().fetchFullPayload();
233 if (!job->exec()) {
234 return contacts;
237 foreach (const Akonadi::Item &item, job->items()) {
238 if (item.hasPayload<KContacts::Addressee>()) {
239 contacts.append(item.payload<KContacts::Addressee>());
244 return contacts;