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

akonadi

  • sources
  • kde-4.12
  • kdepimlibs
  • 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  // FIXME: Temporary until EntityTreeView compiles
97 #ifndef Q_OS_WINCE
98  Akonadi::EntityTreeView *mView;
99 #else
100  QTreeView* mView;
101 #endif
102  EmailAddressSelectionProxyModel *mSelectionModel;
103 };
104 
105 void EmailAddressSelectionWidget::Private::init()
106 {
107  KGlobal::locale()->insertCatalog( QLatin1String( "akonadicontact" ) );
108  // setup internal model if needed
109  if ( !mModel ) {
110  Akonadi::Session *session = new Akonadi::Session( "InternalEmailAddressSelectionWidgetModel", q );
111 
112  Akonadi::ItemFetchScope scope;
113  scope.fetchFullPayload( true );
114  scope.fetchAttribute<Akonadi::EntityDisplayAttribute>();
115 
116  Akonadi::ChangeRecorder *changeRecorder = new Akonadi::ChangeRecorder( q );
117  changeRecorder->setSession( session );
118  changeRecorder->fetchCollection( true );
119  changeRecorder->setItemFetchScope( scope );
120  changeRecorder->setCollectionMonitored( Akonadi::Collection::root() );
121  changeRecorder->setMimeTypeMonitored( KABC::Addressee::mimeType(), true );
122  changeRecorder->setMimeTypeMonitored( KABC::ContactGroup::mimeType(), true );
123 
124  Akonadi::ContactsTreeModel *model = new Akonadi::ContactsTreeModel( changeRecorder, q );
125 // model->setCollectionFetchStrategy( Akonadi::ContactsTreeModel::InvisibleFetch );
126 
127  mModel = model;
128  }
129 
130  // setup ui
131  QVBoxLayout *layout = new QVBoxLayout( q );
132 
133  mDescriptionLabel = new QLabel;
134  mDescriptionLabel->hide();
135  layout->addWidget( mDescriptionLabel );
136 
137  QHBoxLayout *searchLayout = new QHBoxLayout;
138  layout->addLayout( searchLayout );
139 
140  // FIXME: Temporary until EntityTreeView compiles
141 #ifndef Q_OS_WINCE
142  mView = new Akonadi::EntityTreeView;
143 #else
144  mView = new QTreeView;
145 #endif
146 
147  QLabel *label = new QLabel( i18nc( "@label Search in a list of contacts", "Search:" ) );
148  mSearchLine = new SearchLineEdit( mView );
149  label->setBuddy( mSearchLine );
150  searchLayout->addWidget( label );
151  searchLayout->addWidget( mSearchLine );
152 
153 #ifndef QT_NO_DRAGANDDROP
154  mView->setDragDropMode( QAbstractItemView::NoDragDrop );
155 #endif
156  layout->addWidget( mView );
157 
158  Akonadi::ContactsFilterProxyModel *filter = new Akonadi::ContactsFilterProxyModel( q );
159  filter->setFilterFlags( ContactsFilterProxyModel::HasEmail );
160  filter->setExcludeVirtualCollections( true );
161  filter->setSourceModel( mModel );
162 
163  mSelectionModel = new EmailAddressSelectionProxyModel( q );
164  mSelectionModel->setSourceModel( filter );
165 
166  mView->setModel( mSelectionModel );
167  mView->header()->hide();
168 
169  q->connect( mSearchLine, SIGNAL(textChanged(QString)),
170  filter, SLOT(setFilterString(QString)) );
171 
172  // FIXME: Temporary until EntityTreeView compiles
173 #ifndef Q_OS_WINCE
174  q->connect( mView, SIGNAL(doubleClicked(Akonadi::Item)),
175  q, SIGNAL(doubleClicked()));
176 #endif
177  Control::widgetNeedsAkonadi( q );
178 
179  mSearchLine->setFocus();
180 
181  QTimer::singleShot( 1000, mView, SLOT(expandAll()) );
182 }
183 
184 EmailAddressSelectionWidget::EmailAddressSelectionWidget( QWidget * parent )
185  : QWidget( parent ),
186  d( new Private( this, 0 ) )
187 {
188 }
189 
190 EmailAddressSelectionWidget::EmailAddressSelectionWidget( QAbstractItemModel *model, QWidget * parent )
191  : QWidget( parent ),
192  d( new Private( this, model ) )
193 {
194 }
195 
196 EmailAddressSelectionWidget::~EmailAddressSelectionWidget()
197 {
198  delete d;
199 }
200 
201 EmailAddressSelection::List EmailAddressSelectionWidget::selectedAddresses() const
202 {
203  EmailAddressSelection::List selections;
204 
205  if ( !d->mView->selectionModel() ) {
206  return selections;
207  }
208 
209  const QModelIndexList selectedRows = d->mView->selectionModel()->selectedRows( 0 );
210  foreach ( const QModelIndex &index, selectedRows ) {
211  EmailAddressSelection selection;
212  selection.d->mName = index.data( EmailAddressSelectionProxyModel::NameRole ).toString();
213  selection.d->mEmailAddress = index.data( EmailAddressSelectionProxyModel::EmailAddressRole ).toString();
214  selection.d->mItem = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>();
215 
216  if ( !selection.d->mEmailAddress.isEmpty() ) {
217  selections << selection;
218  }
219  }
220 
221  return selections;
222 }
223 
224 KLineEdit* EmailAddressSelectionWidget::searchLineEdit() const
225 {
226  return d->mSearchLine;
227 }
228 
229 QTreeView* EmailAddressSelectionWidget::view() const
230 {
231  return d->mView;
232 }
233 
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:126
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:201
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:229
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:265
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:131
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:196
Akonadi::EmailAddressSelectionWidget::EmailAddressSelectionWidget
EmailAddressSelectionWidget(QWidget *parent=0)
Creates a new email address selection widget.
Definition: emailaddressselectionwidget.cpp:184
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:224
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 Tue Oct 14 2014 23:00:27 by doxygen 1.8.7 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 API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

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