• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.9.4 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 <kglobal.h>
41 
42 #include <QtCore/QTimer>
43 #include <QtGui/QHBoxLayout>
44 #include <QtGui/QHeaderView>
45 #include <QtGui/QKeyEvent>
46 #include <QtGui/QLabel>
47 #include <QtGui/QVBoxLayout>
48 
49 using namespace Akonadi;
50 
54 class SearchLineEdit : public KLineEdit
55 {
56  public:
57  SearchLineEdit( QWidget *receiver, QWidget *parent = 0 )
58  : KLineEdit( parent ), mReceiver( receiver )
59  {
60  }
61 
62  protected:
63  virtual void keyPressEvent( QKeyEvent *event )
64  {
65  if ( event->key() == Qt::Key_Down )
66  QMetaObject::invokeMethod( mReceiver, "setFocus" );
67 
68  KLineEdit::keyPressEvent( event );
69  }
70 
71  private:
72  QWidget *mReceiver;
73 };
74 
78 class EmailAddressSelectionWidget::Private
79 {
80  public:
81  Private( EmailAddressSelectionWidget *qq, QAbstractItemModel *model )
82  : q( qq ), mModel( model )
83  {
84  init();
85  }
86 
87  void init();
88 
89  EmailAddressSelectionWidget *q;
90  QAbstractItemModel *mModel;
91  QLabel *mDescriptionLabel;
92  SearchLineEdit *mSearchLine;
93  // FIXME: Temporary until EntityTreeView compiles
94 #ifndef Q_OS_WINCE
95  Akonadi::EntityTreeView *mView;
96 #else
97  QTreeView* mView;
98 #endif
99  EmailAddressSelectionProxyModel *mSelectionModel;
100 };
101 
102 void EmailAddressSelectionWidget::Private::init()
103 {
104  KGlobal::locale()->insertCatalog( QLatin1String( "akonadicontact" ) );
105  // setup internal model if needed
106  if ( !mModel ) {
107  Akonadi::Session *session = new Akonadi::Session( "InternalEmailAddressSelectionWidgetModel", q );
108 
109  Akonadi::ItemFetchScope scope;
110  scope.fetchFullPayload( true );
111  scope.fetchAttribute<Akonadi::EntityDisplayAttribute>();
112 
113  Akonadi::ChangeRecorder *changeRecorder = new Akonadi::ChangeRecorder( q );
114  changeRecorder->setSession( session );
115  changeRecorder->fetchCollection( true );
116  changeRecorder->setItemFetchScope( scope );
117  changeRecorder->setCollectionMonitored( Akonadi::Collection::root() );
118  changeRecorder->setMimeTypeMonitored( KABC::Addressee::mimeType(), true );
119  changeRecorder->setMimeTypeMonitored( KABC::ContactGroup::mimeType(), true );
120 
121  Akonadi::ContactsTreeModel *model = new Akonadi::ContactsTreeModel( changeRecorder, q );
122 // model->setCollectionFetchStrategy( Akonadi::ContactsTreeModel::InvisibleFetch );
123 
124  mModel = model;
125  }
126 
127  // setup ui
128  QVBoxLayout *layout = new QVBoxLayout( q );
129 
130  mDescriptionLabel = new QLabel;
131  mDescriptionLabel->hide();
132  layout->addWidget( mDescriptionLabel );
133 
134  QHBoxLayout *searchLayout = new QHBoxLayout;
135  layout->addLayout( searchLayout );
136 
137  // FIXME: Temporary until EntityTreeView compiles
138 #ifndef Q_OS_WINCE
139  mView = new Akonadi::EntityTreeView;
140 #else
141  mView = new QTreeView;
142 #endif
143 
144  QLabel *label = new QLabel( i18nc( "@label Search in a list of contacts", "Search:" ) );
145  mSearchLine = new SearchLineEdit( mView );
146  label->setBuddy( mSearchLine );
147  searchLayout->addWidget( label );
148  searchLayout->addWidget( mSearchLine );
149 
150 #ifndef QT_NO_DRAGANDDROP
151  mView->setDragDropMode( QAbstractItemView::NoDragDrop );
152 #endif
153  layout->addWidget( mView );
154 
155  Akonadi::ContactsFilterProxyModel *filter = new Akonadi::ContactsFilterProxyModel( q );
156  filter->setFilterFlags( ContactsFilterProxyModel::HasEmail );
157  filter->setExcludeVirtualCollections( true );
158  filter->setSourceModel( mModel );
159 
160  mSelectionModel = new EmailAddressSelectionProxyModel( q );
161  mSelectionModel->setSourceModel( filter );
162 
163  mView->setModel( mSelectionModel );
164  mView->header()->hide();
165 
166  q->connect( mSearchLine, SIGNAL(textChanged(QString)),
167  filter, SLOT(setFilterString(QString)) );
168 
169  Control::widgetNeedsAkonadi( q );
170 
171  mSearchLine->setFocus();
172 
173  QTimer::singleShot( 1000, mView, SLOT(expandAll()) );
174 }
175 
176 
177 EmailAddressSelectionWidget::EmailAddressSelectionWidget( QWidget * parent )
178  : QWidget( parent ),
179  d( new Private( this, 0 ) )
180 {
181 }
182 
183 EmailAddressSelectionWidget::EmailAddressSelectionWidget( QAbstractItemModel *model, QWidget * parent )
184  : QWidget( parent ),
185  d( new Private( this, model ) )
186 {
187 }
188 
189 EmailAddressSelectionWidget::~EmailAddressSelectionWidget()
190 {
191  delete d;
192 }
193 
194 EmailAddressSelection::List EmailAddressSelectionWidget::selectedAddresses() const
195 {
196  EmailAddressSelection::List selections;
197 
198  if ( !d->mView->selectionModel() )
199  return selections;
200 
201  const QModelIndexList selectedRows = d->mView->selectionModel()->selectedRows( 0 );
202  foreach ( const QModelIndex &index, selectedRows ) {
203  EmailAddressSelection selection;
204  selection.d->mName = index.data( EmailAddressSelectionProxyModel::NameRole ).toString();
205  selection.d->mEmailAddress = index.data( EmailAddressSelectionProxyModel::EmailAddressRole ).toString();
206  selection.d->mItem = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>();
207 
208  if ( !selection.d->mEmailAddress.isEmpty() )
209  selections << selection;
210  }
211 
212  return selections;
213 }
214 
215 KLineEdit* EmailAddressSelectionWidget::searchLineEdit() const
216 {
217  return d->mSearchLine;
218 }
219 
220 QTreeView* EmailAddressSelectionWidget::view() const
221 {
222  return d->mView;
223 }
224 
225 #include "emailaddressselectionwidget.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Tue Dec 11 2012 12:15:28 by doxygen 1.8.1.2 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.9.4 API Reference

Skip menu "kdepimlibs-4.9.4 API Reference"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  •   richtextbuilders
  • 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