23#include "emailaddressselectionwidget.h"
25#include "emailaddressselection_p.h"
26#include "emailaddressselectionproxymodel_p.h"
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>
40#include <klocalizedstring.h>
43#include <QtCore/QTimer>
50using namespace Akonadi;
55class SearchLineEdit :
public KLineEdit
58 SearchLineEdit( QWidget *receiver, QWidget *parent = 0 )
59 : KLineEdit( parent ), mReceiver( receiver )
61 setClearButtonShown(
true );
65 virtual void keyPressEvent( QKeyEvent *event )
67 if ( event->key() == Qt::Key_Down ) {
68 QMetaObject::invokeMethod( mReceiver,
"setFocus" );
71 KLineEdit::keyPressEvent( event );
81class EmailAddressSelectionWidget::Private
85 : q( qq ), mModel( model ), mShowOnlyContactWithEmail(showOnlyContactWithEmail)
93 QAbstractItemModel *mModel;
94 QLabel *mDescriptionLabel;
95 SearchLineEdit *mSearchLine;
96 Akonadi::EntityTreeView *mView;
97 EmailAddressSelectionProxyModel *mSelectionModel;
98 bool mShowOnlyContactWithEmail;
101void EmailAddressSelectionWidget::Private::init()
103 KGlobal::locale()->insertCatalog( QLatin1String(
"akonadicontact" ) );
106 Akonadi::Session *session =
new Akonadi::Session(
"InternalEmailAddressSelectionWidgetModel", q );
108 Akonadi::ItemFetchScope scope;
109 scope.fetchFullPayload(
true );
110 scope.fetchAttribute<Akonadi::EntityDisplayAttribute>();
112 Akonadi::ChangeRecorder *changeRecorder =
new Akonadi::ChangeRecorder( q );
113 changeRecorder->setSession( session );
114 changeRecorder->fetchCollection(
true );
115 changeRecorder->setItemFetchScope( scope );
116 changeRecorder->setCollectionMonitored( Akonadi::Collection::root() );
117 changeRecorder->setMimeTypeMonitored( KABC::Addressee::mimeType(),
true );
118 changeRecorder->setMimeTypeMonitored( KABC::ContactGroup::mimeType(),
true );
120 Akonadi::ContactsTreeModel *model =
new Akonadi::ContactsTreeModel( changeRecorder, q );
127 QVBoxLayout *layout =
new QVBoxLayout( q );
129 mDescriptionLabel =
new QLabel;
130 mDescriptionLabel->hide();
131 layout->addWidget( mDescriptionLabel );
133 QHBoxLayout *searchLayout =
new QHBoxLayout;
134 layout->addLayout( searchLayout );
136 mView =
new Akonadi::EntityTreeView;
138 QLabel *label =
new QLabel( i18nc(
"@label Search in a list of contacts",
"Search:" ) );
139 mSearchLine =
new SearchLineEdit( mView );
140 label->setBuddy( mSearchLine );
141 searchLayout->addWidget( label );
142 searchLayout->addWidget( mSearchLine );
144#ifndef QT_NO_DRAGANDDROP
145 mView->setDragDropMode( QAbstractItemView::NoDragDrop );
147 layout->addWidget( mView );
149 Akonadi::ContactsFilterProxyModel *filter =
new Akonadi::ContactsFilterProxyModel( q );
150 if (mShowOnlyContactWithEmail)
153 filter->setSourceModel( mModel );
155 mSelectionModel =
new EmailAddressSelectionProxyModel( q );
156 mSelectionModel->setSourceModel( filter );
158 mView->setModel( mSelectionModel );
159 mView->header()->hide();
161 q->connect( mSearchLine, SIGNAL(textChanged(QString)),
162 filter, SLOT(setFilterString(QString)) );
166 Control::widgetNeedsAkonadi( q );
168 mSearchLine->setFocus();
170 QTimer::singleShot( 1000, mView, SLOT(expandAll()) );
175 d( new Private( true, this, 0 ) )
181 d( new Private( true, this, model ) )
187 d( new Private( showOnlyContactWithEmail ,this, model ) )
200 if ( !d->mView->selectionModel() ) {
204 const QModelIndexList selectedRows = d->mView->selectionModel()->selectedRows( 0 );
205 foreach (
const QModelIndex &index, selectedRows ) {
207 selection.d->mName = index.data( EmailAddressSelectionProxyModel::NameRole ).toString();
208 selection.d->mEmailAddress = index.data( EmailAddressSelectionProxyModel::EmailAddressRole ).toString();
209 selection.d->mItem = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>();
211 if ( d->mShowOnlyContactWithEmail ) {
212 if ( !selection.d->mEmailAddress.isEmpty() ) {
213 selections << selection;
216 selections << selection;
225 return d->mSearchLine;
An selection of an email address and corresponding name.
QList< EmailAddressSelection > List
A list of email address selection objects.