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

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