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

kaddressbook

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

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