• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kaddressbook

  • sources
  • kde-4.12
  • kdepim
  • kaddressbook
contactselectionwidget.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
3 
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.
8 
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.
13 
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.
18 */
19 
20 #include "contactselectionwidget.h"
21 
22 #include <Akonadi/CollectionComboBox>
23 #include <Akonadi/EntityTreeModel>
24 #include <Akonadi/ItemFetchJob>
25 #include <Akonadi/ItemFetchScope>
26 #include <Akonadi/RecursiveItemFetchJob>
27 
28 #include <KLocale>
29 
30 #include <QButtonGroup>
31 #include <QCheckBox>
32 #include <QGridLayout>
33 #include <QGroupBox>
34 #include <QItemSelectionModel>
35 #include <QLabel>
36 #include <QRadioButton>
37 #include <QVBoxLayout>
38 
39 ContactSelectionWidget::ContactSelectionWidget( QItemSelectionModel *selectionModel,
40  QWidget *parent )
41  : QWidget( parent ), mSelectionModel( selectionModel )
42 {
43  initGui();
44 
45  mSelectedContactsButton->setEnabled( mSelectionModel->hasSelection() );
46  mAddressBookSelection->setEnabled( false );
47  mAddressBookSelectionRecursive->setEnabled( false );
48 
49  connect( mAddressBookContactsButton, SIGNAL(toggled(bool)),
50  mAddressBookSelection, SLOT(setEnabled(bool)) );
51  connect( mAddressBookContactsButton, SIGNAL(toggled(bool)),
52  mAddressBookSelectionRecursive, SLOT(setEnabled(bool)) );
53 
54  // apply default configuration
55  if ( mSelectionModel->hasSelection() ) {
56  mSelectedContactsButton->setChecked( true );
57  } else {
58  mAllContactsButton->setChecked( true );
59  }
60 }
61 
62 void ContactSelectionWidget::setMessageText( const QString &message )
63 {
64  mMessageLabel->setText( message );
65 }
66 
67 void ContactSelectionWidget::setDefaultAddressBook( const Akonadi::Collection &addressBook )
68 {
69  mAddressBookSelection->setDefaultCollection( addressBook );
70 }
71 
72 KABC::Addressee::List ContactSelectionWidget::selectedContacts() const
73 {
74  if ( mAllContactsButton->isChecked() ) {
75  return collectAllContacts();
76  } else if ( mSelectedContactsButton->isChecked() ) {
77  return collectSelectedContacts();
78  } else if ( mAddressBookContactsButton->isChecked() ) {
79  return collectAddressBookContacts();
80  }
81 
82  return KABC::Addressee::List();
83 }
84 
85 void ContactSelectionWidget::initGui()
86 {
87  QVBoxLayout *layout = new QVBoxLayout( this );
88 
89  mMessageLabel = new QLabel;
90  layout->addWidget( mMessageLabel );
91 
92  QButtonGroup *group = new QButtonGroup( this );
93 
94  QGroupBox *groupBox = new QGroupBox;
95 
96  QGridLayout *boxLayout = new QGridLayout;
97  groupBox->setLayout( boxLayout );
98 
99  mAllContactsButton = new QRadioButton( i18nc( "@option:radio", "All contacts" ) );
100  mAllContactsButton->setToolTip(
101  i18nc( "@info:tooltip", "All contacts from all your address books" ) );
102  mAllContactsButton->setWhatsThis(
103  i18nc( "@info:whatsthis",
104  "Choose this option you want to select all your contacts from "
105  "all your address books." ) );
106 
107  mSelectedContactsButton = new QRadioButton( i18nc( "@option:radio","Selected contacts" ) );
108  mSelectedContactsButton->setToolTip(
109  i18nc( "@info:tooltip", "Only the contacts currently selected" ) );
110  mSelectedContactsButton->setWhatsThis(
111  i18nc( "@info:whatsthis",
112  "Choose this option if you want only the contacts you have already "
113  "selected in the graphical interface." ) );
114 
115  mAddressBookContactsButton = new QRadioButton( i18nc( "@option:radio", "All contacts from:" ) );
116  mAddressBookContactsButton->setToolTip(
117  i18nc( "@info:tooltip", "All contacts from a chosen address book" ) );
118  mAddressBookContactsButton->setWhatsThis(
119  i18nc( "@info:whatsthis",
120  "Choose this option if you want to select all the contacts from only one "
121  "of your address books. Once this option is clicked you will be provided "
122  "a drop down box listing all those address books and permitted to select "
123  "the one you want." ) );
124 
125  mAddressBookSelection = new Akonadi::CollectionComboBox;
126  mAddressBookSelection->setMimeTypeFilter( QStringList() << KABC::Addressee::mimeType() );
127  mAddressBookSelection->setAccessRightsFilter( Akonadi::Collection::ReadOnly );
128  mAddressBookSelection->setExcludeVirtualCollections( true );
129  mAddressBookSelectionRecursive = new QCheckBox( i18nc( "@option:check", "Include Subfolders" ) );
130  mAddressBookSelectionRecursive->setToolTip(
131  i18nc( "@info:tooltip", "Select all subfolders including the top-level folder" ) );
132  mAddressBookSelectionRecursive->setWhatsThis(
133  i18nc( "@info:whatsthis",
134  "Check this box if you want to select all contacts from this folder, "
135  "including all subfolders. If you only want the contacts from the "
136  "top-level folder then leave this box unchecked." ) );
137 
138  group->addButton( mAllContactsButton );
139  group->addButton( mSelectedContactsButton );
140  group->addButton( mAddressBookContactsButton );
141 
142  boxLayout->addWidget( mAllContactsButton, 0, 0, 1, 2 );
143  boxLayout->addWidget( mSelectedContactsButton, 1, 0, 1, 2 );
144  boxLayout->addWidget( mAddressBookContactsButton, 2, 0, Qt::AlignTop );
145 
146  QVBoxLayout *addressBookLayout = new QVBoxLayout;
147  addressBookLayout->setMargin( 0 );
148  addressBookLayout->addWidget( mAddressBookSelection );
149  addressBookLayout->addWidget( mAddressBookSelectionRecursive );
150 
151  boxLayout->addLayout( addressBookLayout, 2, 1 );
152 
153  layout->addWidget( groupBox );
154  layout->addStretch( 1 );
155 }
156 
157 KABC::Addressee::List ContactSelectionWidget::collectAllContacts() const
158 {
159  Akonadi::RecursiveItemFetchJob *job =
160  new Akonadi::RecursiveItemFetchJob( Akonadi::Collection::root(),
161  QStringList() << KABC::Addressee::mimeType() );
162  job->fetchScope().fetchFullPayload();
163 
164  KABC::Addressee::List contacts;
165  if ( !job->exec() ) {
166  return contacts;
167  }
168 
169  foreach ( const Akonadi::Item &item, job->items() ) {
170  if ( item.isValid() && item.hasPayload<KABC::Addressee>() ) {
171  contacts.append( item.payload<KABC::Addressee>() );
172  }
173  }
174 
175  return contacts;
176 }
177 
178 KABC::Addressee::List ContactSelectionWidget::collectSelectedContacts() const
179 {
180  KABC::Addressee::List contacts;
181 
182  const QModelIndexList indexes = mSelectionModel->selectedRows( 0 );
183  for ( int i = 0; i < indexes.count(); ++i ) {
184  const QModelIndex index = indexes.at( i );
185  if ( index.isValid() ) {
186  const Akonadi::Item item =
187  index.data( Akonadi::EntityTreeModel::ItemRole ).value<Akonadi::Item>();
188  if ( item.isValid() && item.hasPayload<KABC::Addressee>() ) {
189  contacts.append( item.payload<KABC::Addressee>() );
190  }
191  }
192  }
193 
194  return contacts;
195 }
196 
197 KABC::Addressee::List ContactSelectionWidget::collectAddressBookContacts() const
198 {
199  KABC::Addressee::List contacts;
200 
201  const Akonadi::Collection collection = mAddressBookSelection->currentCollection();
202  if ( !collection.isValid() ) {
203  return contacts;
204  }
205 
206  if ( mAddressBookSelectionRecursive->isChecked() ) {
207  Akonadi::RecursiveItemFetchJob *job =
208  new Akonadi::RecursiveItemFetchJob( collection,
209  QStringList() << KABC::Addressee::mimeType() );
210  job->fetchScope().fetchFullPayload();
211 
212  if ( !job->exec() ) {
213  return contacts;
214  }
215 
216  foreach ( const Akonadi::Item &item, job->items() ) {
217  if ( item.hasPayload<KABC::Addressee>() ) {
218  contacts.append( item.payload<KABC::Addressee>() );
219  }
220  }
221  } else {
222  Akonadi::ItemFetchJob *job = new Akonadi::ItemFetchJob( collection );
223  job->fetchScope().fetchFullPayload();
224 
225  if ( !job->exec() ) {
226  return contacts;
227  }
228 
229  foreach ( const Akonadi::Item &item, job->items() ) {
230  if ( item.hasPayload<KABC::Addressee>() ) {
231  contacts.append( item.payload<KABC::Addressee>() );
232  }
233  }
234  }
235 
236  return contacts;
237 }
238 
239 #include "contactselectionwidget.moc"
ContactSelectionWidget::setDefaultAddressBook
void setDefaultAddressBook(const Akonadi::Collection &addressBook)
Sets the default addressbook.
Definition: contactselectionwidget.cpp:67
ContactSelectionWidget::selectedContacts
KABC::Addressee::List selectedContacts() const
Returns the list of selected contacts.
Definition: contactselectionwidget.cpp:72
ContactSelectionWidget::setMessageText
void setMessageText(const QString &message)
Sets the message text.
Definition: contactselectionwidget.cpp:62
ContactSelectionWidget::ContactSelectionWidget
ContactSelectionWidget(QItemSelectionModel *selectionModel, QWidget *parent=0)
Creates a new contact selection widget.
Definition: contactselectionwidget.cpp:39
contactselectionwidget.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:55:51 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kaddressbook

Skip menu "kaddressbook"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal