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

akonadi/contact

  • 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
50using namespace Akonadi;
51
55class 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
81class EmailAddressSelectionWidget::Private
82{
83 public:
84 Private( bool showOnlyContactWithEmail, EmailAddressSelectionWidget *qq, QAbstractItemModel *model )
85 : q( qq ), mModel( model ), mShowOnlyContactWithEmail(showOnlyContactWithEmail)
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 bool mShowOnlyContactWithEmail;
99};
100
101void EmailAddressSelectionWidget::Private::init()
102{
103 KGlobal::locale()->insertCatalog( QLatin1String( "akonadicontact" ) );
104 // setup internal model if needed
105 if ( !mModel ) {
106 Akonadi::Session *session = new Akonadi::Session( "InternalEmailAddressSelectionWidgetModel", q );
107
108 Akonadi::ItemFetchScope scope;
109 scope.fetchFullPayload( true );
110 scope.fetchAttribute<Akonadi::EntityDisplayAttribute>();
111
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 );
119
120 Akonadi::ContactsTreeModel *model = new Akonadi::ContactsTreeModel( changeRecorder, q );
121// model->setCollectionFetchStrategy( Akonadi::ContactsTreeModel::InvisibleFetch );
122
123 mModel = model;
124 }
125
126 // setup ui
127 QVBoxLayout *layout = new QVBoxLayout( q );
128
129 mDescriptionLabel = new QLabel;
130 mDescriptionLabel->hide();
131 layout->addWidget( mDescriptionLabel );
132
133 QHBoxLayout *searchLayout = new QHBoxLayout;
134 layout->addLayout( searchLayout );
135
136 mView = new Akonadi::EntityTreeView;
137
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 );
143
144#ifndef QT_NO_DRAGANDDROP
145 mView->setDragDropMode( QAbstractItemView::NoDragDrop );
146#endif
147 layout->addWidget( mView );
148
149 Akonadi::ContactsFilterProxyModel *filter = new Akonadi::ContactsFilterProxyModel( q );
150 if (mShowOnlyContactWithEmail)
151 filter->setFilterFlags( ContactsFilterProxyModel::HasEmail );
152 filter->setExcludeVirtualCollections( true );
153 filter->setSourceModel( mModel );
154
155 mSelectionModel = new EmailAddressSelectionProxyModel( q );
156 mSelectionModel->setSourceModel( filter );
157
158 mView->setModel( mSelectionModel );
159 mView->header()->hide();
160
161 q->connect( mSearchLine, SIGNAL(textChanged(QString)),
162 filter, SLOT(setFilterString(QString)) );
163
164 q->connect( mView, SIGNAL(doubleClicked(Akonadi::Item)),
165 q, SIGNAL(doubleClicked()));
166 Control::widgetNeedsAkonadi( q );
167
168 mSearchLine->setFocus();
169
170 QTimer::singleShot( 1000, mView, SLOT(expandAll()) );
171}
172
173EmailAddressSelectionWidget::EmailAddressSelectionWidget( QWidget * parent )
174 : QWidget( parent ),
175 d( new Private( true, this, 0 ) )
176{
177}
178
179EmailAddressSelectionWidget::EmailAddressSelectionWidget( QAbstractItemModel *model, QWidget * parent )
180 : QWidget( parent ),
181 d( new Private( true, this, model ) )
182{
183}
184
185EmailAddressSelectionWidget::EmailAddressSelectionWidget( bool showOnlyContactWithEmail, QAbstractItemModel *model, QWidget * parent )
186 : QWidget( parent ),
187 d( new Private( showOnlyContactWithEmail ,this, model ) )
188{
189}
190
191EmailAddressSelectionWidget::~EmailAddressSelectionWidget()
192{
193 delete d;
194}
195
196EmailAddressSelection::List EmailAddressSelectionWidget::selectedAddresses() const
197{
198 EmailAddressSelection::List selections;
199
200 if ( !d->mView->selectionModel() ) {
201 return selections;
202 }
203
204 const QModelIndexList selectedRows = d->mView->selectionModel()->selectedRows( 0 );
205 foreach ( const QModelIndex &index, selectedRows ) {
206 EmailAddressSelection selection;
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>();
210
211 if ( d->mShowOnlyContactWithEmail ) {
212 if ( !selection.d->mEmailAddress.isEmpty() ) {
213 selections << selection;
214 }
215 } else {
216 selections << selection;
217 }
218 }
219
220 return selections;
221}
222
223KLineEdit* EmailAddressSelectionWidget::searchLineEdit() const
224{
225 return d->mSearchLine;
226}
227
228QTreeView* EmailAddressSelectionWidget::view() const
229{
230 return d->mView;
231}
232
Akonadi::ContactsFilterProxyModel
A proxy model for ContactsTreeModel models.
Definition: contactsfilterproxymodel.h:61
Akonadi::ContactsFilterProxyModel::setExcludeVirtualCollections
void setExcludeVirtualCollections(bool exclude)
Sets whether we want virtual collections to be filtered or not.
Definition: contactsfilterproxymodel.cpp:129
Akonadi::ContactsFilterProxyModel::setFilterFlags
void setFilterFlags(ContactsFilterProxyModel::FilterFlags flags)
Sets the filter flags.
Definition: contactsfilterproxymodel.cpp:124
Akonadi::ContactsTreeModel
A model for contacts and contact groups as available in Akonadi.
Definition: contactstreemodel.h:79
Akonadi::EmailAddressSelectionWidget
A widget to select email addresses from Akonadi.
Definition: emailaddressselectionwidget.h:67
Akonadi::EmailAddressSelectionWidget::EmailAddressSelectionWidget
EmailAddressSelectionWidget(QWidget *parent=0)
Creates a new email address selection widget.
Definition: emailaddressselectionwidget.cpp:173
Akonadi::EmailAddressSelectionWidget::searchLineEdit
KLineEdit * searchLineEdit() const
Returns the line edit that is used for the search line.
Definition: emailaddressselectionwidget.cpp:223
Akonadi::EmailAddressSelectionWidget::selectedAddresses
EmailAddressSelection::List selectedAddresses() const
Returns the list of selected email addresses.
Definition: emailaddressselectionwidget.cpp:196
Akonadi::EmailAddressSelectionWidget::view
QTreeView * view() const
Returns the tree view that is used to list the items.
Definition: emailaddressselectionwidget.cpp:228
Akonadi::EmailAddressSelectionWidget::~EmailAddressSelectionWidget
~EmailAddressSelectionWidget()
Destroys the email address selection widget.
Definition: emailaddressselectionwidget.cpp:191
Akonadi::EmailAddressSelection
An selection of an email address and corresponding name.
Definition: emailaddressselection.h:50
Akonadi::EmailAddressSelection::List
QList< EmailAddressSelection > List
A list of email address selection objects.
Definition: emailaddressselection.h:55
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu Jul 21 2022 00:00:00 by doxygen 1.9.5 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi/contact

Skip menu "akonadi/contact"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs-4.14.10 API Reference

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