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
20 #include "contactselectionwidget.h"
23 #include <Akonadi/CollectionComboBox>
24 #include <Akonadi/EntityTreeModel>
25 #include <Akonadi/ItemFetchJob>
26 #include <Akonadi/ItemFetchScope>
27 #include <Akonadi/RecursiveItemFetchJob>
31 #include <QButtonGroup>
33 #include <QGridLayout>
35 #include <QItemSelectionModel>
37 #include <QRadioButton>
38 #include <QVBoxLayout>
40 ContactSelectionWidget::ContactSelectionWidget( QItemSelectionModel
*selectionModel
,
42 : QWidget( parent
), mSelectionModel( selectionModel
)
46 mSelectedContactsButton
->setEnabled( mSelectionModel
->hasSelection() );
47 mAddressBookSelection
->setEnabled( false );
48 mAddressBookSelectionRecursive
->setEnabled( false );
50 connect( mAddressBookContactsButton
, SIGNAL(toggled(bool)),
51 mAddressBookSelection
, SLOT(setEnabled(bool)) );
52 connect( mAddressBookContactsButton
, SIGNAL(toggled(bool)),
53 mAddressBookSelectionRecursive
, SLOT(setEnabled(bool)) );
55 // apply default configuration
56 if ( mSelectionModel
->hasSelection() ) {
57 mSelectedContactsButton
->setChecked( true );
59 mAllContactsButton
->setChecked( true );
63 void ContactSelectionWidget::setMessageText( const QString
&message
)
65 mMessageLabel
->setText( message
);
68 void ContactSelectionWidget::setDefaultAddressBook( const Akonadi::Collection
&addressBook
)
70 mAddressBookSelection
->setDefaultCollection( addressBook
);
73 KABC::Addressee::List
ContactSelectionWidget::selectedContacts() const
75 if ( mAllContactsButton
->isChecked() ) {
76 return collectAllContacts();
77 } else if ( mSelectedContactsButton
->isChecked() ) {
78 return collectSelectedContacts();
79 } else if ( mAddressBookContactsButton
->isChecked() ) {
80 return collectAddressBookContacts();
83 return KABC::Addressee::List();
86 Akonadi::Item::List
ContactSelectionWidget::selectedContactsItem() const
88 if ( mAllContactsButton
->isChecked() ) {
89 return collectAllContactsItem();
90 } else if ( mSelectedContactsButton
->isChecked() ) {
91 return collectSelectedContactsItem();
92 } else if ( mAddressBookContactsButton
->isChecked() ) {
93 return collectAddressBookContactsItem();
96 return Akonadi::Item::List();
100 void ContactSelectionWidget::initGui()
102 QVBoxLayout
*layout
= new QVBoxLayout( this );
104 mMessageLabel
= new QLabel
;
105 layout
->addWidget( mMessageLabel
);
107 QButtonGroup
*group
= new QButtonGroup( this );
109 QGroupBox
*groupBox
= new QGroupBox
;
111 QGridLayout
*boxLayout
= new QGridLayout
;
112 groupBox
->setLayout( boxLayout
);
114 mAllContactsButton
= new QRadioButton( i18nc( "@option:radio", "All contacts" ) );
115 mAllContactsButton
->setToolTip(
116 i18nc( "@info:tooltip", "All contacts from all your address books" ) );
117 mAllContactsButton
->setWhatsThis(
118 i18nc( "@info:whatsthis",
119 "Choose this option you want to select all your contacts from "
120 "all your address books." ) );
122 mSelectedContactsButton
= new QRadioButton( i18nc( "@option:radio","Selected contacts" ) );
123 mSelectedContactsButton
->setToolTip(
124 i18nc( "@info:tooltip", "Only the contacts currently selected" ) );
125 mSelectedContactsButton
->setWhatsThis(
126 i18nc( "@info:whatsthis",
127 "Choose this option if you want only the contacts you have already "
128 "selected in the graphical interface." ) );
130 mAddressBookContactsButton
= new QRadioButton( i18nc( "@option:radio", "All contacts from:" ) );
131 mAddressBookContactsButton
->setToolTip(
132 i18nc( "@info:tooltip", "All contacts from a chosen address book" ) );
133 mAddressBookContactsButton
->setWhatsThis(
134 i18nc( "@info:whatsthis",
135 "Choose this option if you want to select all the contacts from only one "
136 "of your address books. Once this option is clicked you will be provided "
137 "a drop down box listing all those address books and permitted to select "
138 "the one you want." ) );
140 mAddressBookSelection
= new Akonadi::CollectionComboBox
;
141 mAddressBookSelection
->setMimeTypeFilter( QStringList() << KABC::Addressee::mimeType() );
142 mAddressBookSelection
->setAccessRightsFilter( Akonadi::Collection::ReadOnly
);
143 mAddressBookSelection
->setExcludeVirtualCollections( true );
144 mAddressBookSelectionRecursive
= new QCheckBox( i18nc( "@option:check", "Include Subfolders" ) );
145 mAddressBookSelectionRecursive
->setToolTip(
146 i18nc( "@info:tooltip", "Select all subfolders including the top-level folder" ) );
147 mAddressBookSelectionRecursive
->setWhatsThis(
148 i18nc( "@info:whatsthis",
149 "Check this box if you want to select all contacts from this folder, "
150 "including all subfolders. If you only want the contacts from the "
151 "top-level folder then leave this box unchecked." ) );
153 group
->addButton( mAllContactsButton
);
154 group
->addButton( mSelectedContactsButton
);
155 group
->addButton( mAddressBookContactsButton
);
157 boxLayout
->addWidget( mAllContactsButton
, 0, 0, 1, 2 );
158 boxLayout
->addWidget( mSelectedContactsButton
, 1, 0, 1, 2 );
159 boxLayout
->addWidget( mAddressBookContactsButton
, 2, 0, Qt::AlignTop
);
161 QVBoxLayout
*addressBookLayout
= new QVBoxLayout
;
162 addressBookLayout
->setMargin( 0 );
163 addressBookLayout
->addWidget( mAddressBookSelection
);
164 addressBookLayout
->addWidget( mAddressBookSelectionRecursive
);
166 boxLayout
->addLayout( addressBookLayout
, 2, 1 );
168 layout
->addWidget( groupBox
);
169 layout
->addStretch( 1 );
172 KABC::Addressee::List
ContactSelectionWidget::collectAllContacts() const
174 Akonadi::RecursiveItemFetchJob
*job
=
175 new Akonadi::RecursiveItemFetchJob( Akonadi::Collection::root(),
176 QStringList() << KABC::Addressee::mimeType() );
177 job
->fetchScope().fetchFullPayload();
179 KABC::Addressee::List contacts
;
180 if ( !job
->exec() ) {
184 foreach ( const Akonadi::Item
&item
, job
->items() ) {
185 if ( item
.isValid() && item
.hasPayload
<KABC::Addressee
>() ) {
186 contacts
.append( item
.payload
<KABC::Addressee
>() );
193 Akonadi::Item::List
ContactSelectionWidget::collectAllContactsItem() const
195 Akonadi::RecursiveItemFetchJob
*job
=
196 new Akonadi::RecursiveItemFetchJob( Akonadi::Collection::root(),
197 QStringList() << KABC::Addressee::mimeType() );
198 job
->fetchScope().fetchFullPayload();
200 Akonadi::Item::List lst
;
201 if ( !job
->exec() ) {
205 foreach ( const Akonadi::Item
&item
, job
->items() ) {
206 if ( item
.isValid() && item
.hasPayload
<KABC::Addressee
>() ) {
214 Akonadi::Item::List
ContactSelectionWidget::collectSelectedContactsItem() const
216 Akonadi::Item::List lst
= Utils::collectSelectedContactsItem(mSelectionModel
);
221 KABC::Addressee::List
ContactSelectionWidget::collectSelectedContacts() const
223 KABC::Addressee::List contacts
;
225 const QModelIndexList indexes
= mSelectionModel
->selectedRows( 0 );
226 for ( int i
= 0; i
< indexes
.count(); ++i
) {
227 const QModelIndex index
= indexes
.at( i
);
228 if ( index
.isValid() ) {
229 const Akonadi::Item item
=
230 index
.data( Akonadi::EntityTreeModel::ItemRole
).value
<Akonadi::Item
>();
231 if ( item
.isValid() && item
.hasPayload
<KABC::Addressee
>() ) {
232 contacts
.append( item
.payload
<KABC::Addressee
>() );
240 KABC::Addressee::List
ContactSelectionWidget::collectAddressBookContacts() const
242 KABC::Addressee::List contacts
;
244 const Akonadi::Collection collection
= mAddressBookSelection
->currentCollection();
245 if ( !collection
.isValid() ) {
249 if ( mAddressBookSelectionRecursive
->isChecked() ) {
250 Akonadi::RecursiveItemFetchJob
*job
=
251 new Akonadi::RecursiveItemFetchJob( collection
,
252 QStringList() << KABC::Addressee::mimeType() );
253 job
->fetchScope().fetchFullPayload();
255 if ( !job
->exec() ) {
259 foreach ( const Akonadi::Item
&item
, job
->items() ) {
260 if ( item
.hasPayload
<KABC::Addressee
>() ) {
261 contacts
.append( item
.payload
<KABC::Addressee
>() );
265 Akonadi::ItemFetchJob
*job
= new Akonadi::ItemFetchJob( collection
);
266 job
->fetchScope().fetchFullPayload();
268 if ( !job
->exec() ) {
272 foreach ( const Akonadi::Item
&item
, job
->items() ) {
273 if ( item
.hasPayload
<KABC::Addressee
>() ) {
274 contacts
.append( item
.payload
<KABC::Addressee
>() );
283 Akonadi::Item::List
ContactSelectionWidget::collectAddressBookContactsItem() const
285 Akonadi::Item::List lst
;
287 const Akonadi::Collection collection
= mAddressBookSelection
->currentCollection();
288 if ( !collection
.isValid() ) {
292 if ( mAddressBookSelectionRecursive
->isChecked() ) {
293 Akonadi::RecursiveItemFetchJob
*job
=
294 new Akonadi::RecursiveItemFetchJob( collection
,
295 QStringList() << KABC::Addressee::mimeType() );
296 job
->fetchScope().fetchFullPayload();
298 if ( !job
->exec() ) {
302 foreach ( const Akonadi::Item
&item
, job
->items() ) {
303 if ( item
.hasPayload
<KABC::Addressee
>() ) {
308 Akonadi::ItemFetchJob
*job
= new Akonadi::ItemFetchJob( collection
);
309 job
->fetchScope().fetchFullPayload();
311 if ( !job
->exec() ) {
315 foreach ( const Akonadi::Item
&item
, job
->items() ) {
316 if ( item
.hasPayload
<KABC::Addressee
>() ) {