• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.11.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  }
62 
63  protected:
64  virtual void keyPressEvent( QKeyEvent *event )
65  {
66  if ( event->key() == Qt::Key_Down ) {
67  QMetaObject::invokeMethod( mReceiver, "setFocus" );
68  }
69 
70  KLineEdit::keyPressEvent( event );
71  }
72 
73  private:
74  QWidget *mReceiver;
75 };
76 
80 class EmailAddressSelectionWidget::Private
81 {
82  public:
83  Private( EmailAddressSelectionWidget *qq, QAbstractItemModel *model )
84  : q( qq ), mModel( model )
85  {
86  init();
87  }
88 
89  void init();
90 
91  EmailAddressSelectionWidget *q;
92  QAbstractItemModel *mModel;
93  QLabel *mDescriptionLabel;
94  SearchLineEdit *mSearchLine;
95  // FIXME: Temporary until EntityTreeView compiles
96 #ifndef Q_OS_WINCE
97  Akonadi::EntityTreeView *mView;
98 #else
99  QTreeView* mView;
100 #endif
101  EmailAddressSelectionProxyModel *mSelectionModel;
102 };
103 
104 void EmailAddressSelectionWidget::Private::init()
105 {
106  KGlobal::locale()->insertCatalog( QLatin1String( "akonadicontact" ) );
107  // setup internal model if needed
108  if ( !mModel ) {
109  Akonadi::Session *session = new Akonadi::Session( "InternalEmailAddressSelectionWidgetModel", q );
110 
111  Akonadi::ItemFetchScope scope;
112  scope.fetchFullPayload( true );
113  scope.fetchAttribute<Akonadi::EntityDisplayAttribute>();
114 
115  Akonadi::ChangeRecorder *changeRecorder = new Akonadi::ChangeRecorder( q );
116  changeRecorder->setSession( session );
117  changeRecorder->fetchCollection( true );
118  changeRecorder->setItemFetchScope( scope );
119  changeRecorder->setCollectionMonitored( Akonadi::Collection::root() );
120  changeRecorder->setMimeTypeMonitored( KABC::Addressee::mimeType(), true );
121  changeRecorder->setMimeTypeMonitored( KABC::ContactGroup::mimeType(), true );
122 
123  Akonadi::ContactsTreeModel *model = new Akonadi::ContactsTreeModel( changeRecorder, q );
124 // model->setCollectionFetchStrategy( Akonadi::ContactsTreeModel::InvisibleFetch );
125 
126  mModel = model;
127  }
128 
129  // setup ui
130  QVBoxLayout *layout = new QVBoxLayout( q );
131 
132  mDescriptionLabel = new QLabel;
133  mDescriptionLabel->hide();
134  layout->addWidget( mDescriptionLabel );
135 
136  QHBoxLayout *searchLayout = new QHBoxLayout;
137  layout->addLayout( searchLayout );
138 
139  // FIXME: Temporary until EntityTreeView compiles
140 #ifndef Q_OS_WINCE
141  mView = new Akonadi::EntityTreeView;
142 #else
143  mView = new QTreeView;
144 #endif
145 
146  QLabel *label = new QLabel( i18nc( "@label Search in a list of contacts", "Search:" ) );
147  mSearchLine = new SearchLineEdit( mView );
148  label->setBuddy( mSearchLine );
149  searchLayout->addWidget( label );
150  searchLayout->addWidget( mSearchLine );
151 
152 #ifndef QT_NO_DRAGANDDROP
153  mView->setDragDropMode( QAbstractItemView::NoDragDrop );
154 #endif
155  layout->addWidget( mView );
156 
157  Akonadi::ContactsFilterProxyModel *filter = new Akonadi::ContactsFilterProxyModel( q );
158  filter->setFilterFlags( ContactsFilterProxyModel::HasEmail );
159  filter->setExcludeVirtualCollections( true );
160  filter->setSourceModel( mModel );
161 
162  mSelectionModel = new EmailAddressSelectionProxyModel( q );
163  mSelectionModel->setSourceModel( filter );
164 
165  mView->setModel( mSelectionModel );
166  mView->header()->hide();
167 
168  q->connect( mSearchLine, SIGNAL(textChanged(QString)),
169  filter, SLOT(setFilterString(QString)) );
170 
171  // FIXME: Temporary until EntityTreeView compiles
172 #ifndef Q_OS_WINCE
173  q->connect( mView, SIGNAL(doubleClicked(Akonadi::Item)),
174  q, SIGNAL(doubleClicked()));
175 #endif
176  Control::widgetNeedsAkonadi( q );
177 
178  mSearchLine->setFocus();
179 
180  QTimer::singleShot( 1000, mView, SLOT(expandAll()) );
181 }
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:261
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-2013 The KDE developers.
Generated on Tue Nov 26 2013 09:03:17 by doxygen 1.8.5 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.11.3 API Reference

Skip menu "kdepimlibs-4.11.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