• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.13.3 API Reference
  • KDE Home
  • Contact Us
 

akonadi

  • akonadi
  • contact
emailaddressselectionwidget.cpp
1 /*
2  This file is part of Akonadi Contact.
3 
4  Copyright (c) 2010 KDAB
5  Author: Tobias Koenig <tokoe@kde.org>
6 
7  This library is free software; you can redistribute it and/or modify it
8  under the terms of the GNU Library General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or (at your
10  option) any later version.
11 
12  This library is distributed in the hope that it will be useful, but WITHOUT
13  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
15  License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to the
19  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  02110-1301, USA.
21 */
22 
23 #include "emailaddressselectionwidget.h"
24 
25 #include "emailaddressselection_p.h"
26 #include "emailaddressselectionproxymodel_p.h"
27 
28 #include <akonadi/changerecorder.h>
29 #include <akonadi/contact/contactsfilterproxymodel.h>
30 #include <akonadi/contact/contactstreemodel.h>
31 #include <akonadi/control.h>
32 #include <akonadi/entitydisplayattribute.h>
33 #include <akonadi/entitytreeview.h>
34 #include <akonadi/itemfetchscope.h>
35 #include <akonadi/session.h>
36 #include <kabc/addressee.h>
37 #include <kabc/contactgroup.h>
38 #include <klineedit.h>
39 #include <klocale.h>
40 #include <klocalizedstring.h>
41 #include <kglobal.h>
42 
43 #include <QtCore/QTimer>
44 #include <QHBoxLayout>
45 #include <QHeaderView>
46 #include <QKeyEvent>
47 #include <QLabel>
48 #include <QVBoxLayout>
49 
50 using namespace Akonadi;
51 
55 class SearchLineEdit : public KLineEdit
56 {
57  public:
58  SearchLineEdit( QWidget *receiver, QWidget *parent = 0 )
59  : KLineEdit( parent ), mReceiver( receiver )
60  {
61  setClearButtonShown( true );
62  }
63 
64  protected:
65  virtual void keyPressEvent( QKeyEvent *event )
66  {
67  if ( event->key() == Qt::Key_Down ) {
68  QMetaObject::invokeMethod( mReceiver, "setFocus" );
69  }
70 
71  KLineEdit::keyPressEvent( event );
72  }
73 
74  private:
75  QWidget *mReceiver;
76 };
77 
81 class EmailAddressSelectionWidget::Private
82 {
83  public:
84  Private( EmailAddressSelectionWidget *qq, QAbstractItemModel *model )
85  : q( qq ), mModel( model )
86  {
87  init();
88  }
89 
90  void init();
91 
92  EmailAddressSelectionWidget *q;
93  QAbstractItemModel *mModel;
94  QLabel *mDescriptionLabel;
95  SearchLineEdit *mSearchLine;
96  Akonadi::EntityTreeView *mView;
97  EmailAddressSelectionProxyModel *mSelectionModel;
98 };
99 
100 void EmailAddressSelectionWidget::Private::init()
101 {
102  KGlobal::locale()->insertCatalog( QLatin1String( "akonadicontact" ) );
103  // setup internal model if needed
104  if ( !mModel ) {
105  Akonadi::Session *session = new Akonadi::Session( "InternalEmailAddressSelectionWidgetModel", q );
106 
107  Akonadi::ItemFetchScope scope;
108  scope.fetchFullPayload( true );
109  scope.fetchAttribute<Akonadi::EntityDisplayAttribute>();
110 
111  Akonadi::ChangeRecorder *changeRecorder = new Akonadi::ChangeRecorder( q );
112  changeRecorder->setSession( session );
113  changeRecorder->fetchCollection( true );
114  changeRecorder->setItemFetchScope( scope );
115  changeRecorder->setCollectionMonitored( Akonadi::Collection::root() );
116  changeRecorder->setMimeTypeMonitored( KABC::Addressee::mimeType(), true );
117  changeRecorder->setMimeTypeMonitored( KABC::ContactGroup::mimeType(), true );
118 
119  Akonadi::ContactsTreeModel *model = new Akonadi::ContactsTreeModel( changeRecorder, q );
120 // model->setCollectionFetchStrategy( Akonadi::ContactsTreeModel::InvisibleFetch );
121 
122  mModel = model;
123  }
124 
125  // setup ui
126  QVBoxLayout *layout = new QVBoxLayout( q );
127 
128  mDescriptionLabel = new QLabel;
129  mDescriptionLabel->hide();
130  layout->addWidget( mDescriptionLabel );
131 
132  QHBoxLayout *searchLayout = new QHBoxLayout;
133  layout->addLayout( searchLayout );
134 
135  mView = new Akonadi::EntityTreeView;
136 
137  QLabel *label = new QLabel( i18nc( "@label Search in a list of contacts", "Search:" ) );
138  mSearchLine = new SearchLineEdit( mView );
139  label->setBuddy( mSearchLine );
140  searchLayout->addWidget( label );
141  searchLayout->addWidget( mSearchLine );
142 
143 #ifndef QT_NO_DRAGANDDROP
144  mView->setDragDropMode( QAbstractItemView::NoDragDrop );
145 #endif
146  layout->addWidget( mView );
147 
148  Akonadi::ContactsFilterProxyModel *filter = new Akonadi::ContactsFilterProxyModel( q );
149  filter->setFilterFlags( ContactsFilterProxyModel::HasEmail );
150  filter->setExcludeVirtualCollections( true );
151  filter->setSourceModel( mModel );
152 
153  mSelectionModel = new EmailAddressSelectionProxyModel( q );
154  mSelectionModel->setSourceModel( filter );
155 
156  mView->setModel( mSelectionModel );
157  mView->header()->hide();
158 
159  q->connect( mSearchLine, SIGNAL(textChanged(QString)),
160  filter, SLOT(setFilterString(QString)) );
161 
162  q->connect( mView, SIGNAL(doubleClicked(Akonadi::Item)),
163  q, SIGNAL(doubleClicked()));
164  Control::widgetNeedsAkonadi( q );
165 
166  mSearchLine->setFocus();
167 
168  QTimer::singleShot( 1000, mView, SLOT(expandAll()) );
169 }
170 
171 EmailAddressSelectionWidget::EmailAddressSelectionWidget( QWidget * parent )
172  : QWidget( parent ),
173  d( new Private( this, 0 ) )
174 {
175 }
176 
177 EmailAddressSelectionWidget::EmailAddressSelectionWidget( QAbstractItemModel *model, QWidget * parent )
178  : QWidget( parent ),
179  d( new Private( this, model ) )
180 {
181 }
182 
183 EmailAddressSelectionWidget::~EmailAddressSelectionWidget()
184 {
185  delete d;
186 }
187 
188 EmailAddressSelection::List EmailAddressSelectionWidget::selectedAddresses() const
189 {
190  EmailAddressSelection::List selections;
191 
192  if ( !d->mView->selectionModel() ) {
193  return selections;
194  }
195 
196  const QModelIndexList selectedRows = d->mView->selectionModel()->selectedRows( 0 );
197  foreach ( const QModelIndex &index, selectedRows ) {
198  EmailAddressSelection selection;
199  selection.d->mName = index.data( EmailAddressSelectionProxyModel::NameRole ).toString();
200  selection.d->mEmailAddress = index.data( EmailAddressSelectionProxyModel::EmailAddressRole ).toString();
201  selection.d->mItem = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>();
202 
203  if ( !selection.d->mEmailAddress.isEmpty() ) {
204  selections << selection;
205  }
206  }
207 
208  return selections;
209 }
210 
211 KLineEdit* EmailAddressSelectionWidget::searchLineEdit() const
212 {
213  return d->mSearchLine;
214 }
215 
216 QTreeView* EmailAddressSelectionWidget::view() const
217 {
218  return d->mView;
219 }
220 
Akonadi::ItemFetchScope::fetchAttribute
void fetchAttribute(const QByteArray &type, bool fetch=true)
Sets whether the attribute of the given type should be fetched.
Definition: itemfetchscope.cpp:78
Akonadi::ContactsFilterProxyModel
A proxy model for ContactsTreeModel models.
Definition: contactsfilterproxymodel.h:60
Akonadi::ContactsFilterProxyModel::setFilterFlags
void setFilterFlags(ContactsFilterProxyModel::FilterFlags flags)
Sets the filter flags.
Definition: contactsfilterproxymodel.cpp:124
Akonadi::EmailAddressSelection
An selection of an email address and corresponding name.
Definition: emailaddressselection.h:49
Akonadi::EmailAddressSelection::List
QList< EmailAddressSelection > List
A list of email address selection objects.
Definition: emailaddressselection.h:55
Akonadi::ItemFetchScope::fetchFullPayload
void fetchFullPayload(bool fetch=true)
Sets whether the full payload shall be fetched.
Definition: itemfetchscope.cpp:68
Akonadi::EmailAddressSelectionWidget::selectedAddresses
EmailAddressSelection::List selectedAddresses() const
Returns the list of selected email addresses.
Definition: emailaddressselectionwidget.cpp:188
Akonadi::EntityTreeView
A view to show an item/collection tree provided by an EntityTreeModel.
Definition: entitytreeview.h:71
Akonadi::EmailAddressSelectionWidget::view
QTreeView * view() const
Returns the tree view that is used to list the items.
Definition: emailaddressselectionwidget.cpp:216
Akonadi::Control::widgetNeedsAkonadi
static void widgetNeedsAkonadi(QWidget *widget)
Disable the given widget when Akonadi is not operational and show an error overlay (given enough spac...
Definition: control.cpp:259
Akonadi::EmailAddressSelectionWidget
A widget to select email addresses from Akonadi.
Definition: emailaddressselectionwidget.h:66
Akonadi::Collection::root
static Collection root()
Returns the root collection.
Definition: collection.cpp:192
Akonadi::Session
A communication session with the Akonadi storage.
Definition: session.h:59
Akonadi::ContactsFilterProxyModel::setExcludeVirtualCollections
void setExcludeVirtualCollections(bool exclude)
Sets whether we want virtual collections to be filtered or not.
Definition: contactsfilterproxymodel.cpp:129
Akonadi::ItemFetchScope
Specifies which parts of an item should be fetched from the Akonadi storage.
Definition: itemfetchscope.h:68
Akonadi::EmailAddressSelectionWidget::~EmailAddressSelectionWidget
~EmailAddressSelectionWidget()
Destroys the email address selection widget.
Definition: emailaddressselectionwidget.cpp:183
Akonadi::EmailAddressSelectionWidget::EmailAddressSelectionWidget
EmailAddressSelectionWidget(QWidget *parent=0)
Creates a new email address selection widget.
Definition: emailaddressselectionwidget.cpp:171
Akonadi::ContactsTreeModel
A model for contacts and contact groups as available in Akonadi.
Definition: contactstreemodel.h:78
Akonadi::EntityTreeModel::ItemRole
The Item.
Definition: entitytreemodel.h:331
Akonadi::EmailAddressSelectionWidget::searchLineEdit
KLineEdit * searchLineEdit() const
Returns the line edit that is used for the search line.
Definition: emailaddressselectionwidget.cpp:211
Akonadi::EntityDisplayAttribute
Attribute that stores the properties that are used to display an entity.
Definition: entitydisplayattribute.h:39
Akonadi::ChangeRecorder
Records and replays change notification.
Definition: changerecorder.h:47
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Mon Jul 21 2014 08:03:52 by doxygen 1.8.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

Skip menu "akonadi"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules
  • Related Pages

kdepimlibs-4.13.3 API Reference

Skip menu "kdepimlibs-4.13.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
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