• Skip to content
  • Skip to link menu
KDE 4.7 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • KDE Home
  • Contact Us
 

akonadi/contact

emailaddressselectionwidget.cpp
00001 /*
00002     This file is part of Akonadi Contact.
00003 
00004     Copyright (c) 2010 KDAB
00005     Author: Tobias Koenig <tokoe@kde.org>
00006 
00007     This library is free software; you can redistribute it and/or modify it
00008     under the terms of the GNU Library General Public License as published by
00009     the Free Software Foundation; either version 2 of the License, or (at your
00010     option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful, but WITHOUT
00013     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00015     License for more details.
00016 
00017     You should have received a copy of the GNU Library General Public License
00018     along with this library; see the file COPYING.LIB.  If not, write to the
00019     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00020     02110-1301, USA.
00021 */
00022 
00023 #include "emailaddressselectionwidget.h"
00024 
00025 #include "emailaddressselection_p.h"
00026 #include "emailaddressselectionproxymodel_p.h"
00027 
00028 #include <akonadi/changerecorder.h>
00029 #include <akonadi/contact/contactsfilterproxymodel.h>
00030 #include <akonadi/contact/contactstreemodel.h>
00031 #include <akonadi/control.h>
00032 #include <akonadi/entitydisplayattribute.h>
00033 #include <akonadi/entitytreeview.h>
00034 #include <akonadi/itemfetchscope.h>
00035 #include <akonadi/session.h>
00036 #include <kabc/addressee.h>
00037 #include <kabc/contactgroup.h>
00038 #include <klineedit.h>
00039 #include <klocale.h>
00040 #include <kglobal.h>
00041 
00042 #include <QtCore/QTimer>
00043 #include <QtGui/QHBoxLayout>
00044 #include <QtGui/QHeaderView>
00045 #include <QtGui/QKeyEvent>
00046 #include <QtGui/QLabel>
00047 #include <QtGui/QVBoxLayout>
00048 
00049 using namespace Akonadi;
00050 
00054 class SearchLineEdit : public KLineEdit
00055 {
00056   public:
00057     SearchLineEdit( QWidget *receiver, QWidget *parent = 0 )
00058       : KLineEdit( parent ), mReceiver( receiver )
00059     {
00060     }
00061 
00062   protected:
00063     virtual void keyPressEvent( QKeyEvent *event )
00064     {
00065       if ( event->key() == Qt::Key_Down )
00066         QMetaObject::invokeMethod( mReceiver, "setFocus" );
00067 
00068       KLineEdit::keyPressEvent( event );
00069     }
00070 
00071   private:
00072     QWidget *mReceiver;
00073 };
00074 
00078 class EmailAddressSelectionWidget::Private
00079 {
00080   public:
00081     Private( EmailAddressSelectionWidget *qq, QAbstractItemModel *model )
00082       : q( qq ), mModel( model )
00083     {
00084       init();
00085     }
00086 
00087     void init();
00088 
00089     EmailAddressSelectionWidget *q;
00090     QAbstractItemModel *mModel;
00091     QLabel *mDescriptionLabel;
00092     SearchLineEdit *mSearchLine;
00093     // FIXME: Temporary until EntityTreeView compiles
00094 #ifndef Q_OS_WINCE
00095     Akonadi::EntityTreeView *mView;
00096 #else
00097     QTreeView* mView;
00098 #endif
00099     EmailAddressSelectionProxyModel *mSelectionModel;
00100 };
00101 
00102 void EmailAddressSelectionWidget::Private::init()
00103 {
00104   KGlobal::locale()->insertCatalog( QLatin1String( "akonadicontact" ) );
00105   // setup internal model if needed
00106   if ( !mModel ) {
00107     Akonadi::Session *session = new Akonadi::Session( "InternalEmailAddressSelectionWidgetModel", q );
00108 
00109     Akonadi::ItemFetchScope scope;
00110     scope.fetchFullPayload( true );
00111     scope.fetchAttribute<Akonadi::EntityDisplayAttribute>();
00112 
00113     Akonadi::ChangeRecorder *changeRecorder = new Akonadi::ChangeRecorder( q );
00114     changeRecorder->setSession( session );
00115     changeRecorder->fetchCollection( true );
00116     changeRecorder->setItemFetchScope( scope );
00117     changeRecorder->setCollectionMonitored( Akonadi::Collection::root() );
00118     changeRecorder->setMimeTypeMonitored( KABC::Addressee::mimeType(), true );
00119     changeRecorder->setMimeTypeMonitored( KABC::ContactGroup::mimeType(), true );
00120 
00121     Akonadi::ContactsTreeModel *model = new Akonadi::ContactsTreeModel( changeRecorder, q );
00122 //    model->setCollectionFetchStrategy( Akonadi::ContactsTreeModel::InvisibleFetch );
00123 
00124     mModel = model;
00125   }
00126 
00127   // setup ui
00128   QVBoxLayout *layout = new QVBoxLayout( q );
00129 
00130   mDescriptionLabel = new QLabel;
00131   mDescriptionLabel->hide();
00132   layout->addWidget( mDescriptionLabel );
00133 
00134   QHBoxLayout *searchLayout = new QHBoxLayout;
00135   layout->addLayout( searchLayout );
00136 
00137     // FIXME: Temporary until EntityTreeView compiles
00138 #ifndef Q_OS_WINCE
00139   mView = new Akonadi::EntityTreeView;
00140 #else
00141   mView = new QTreeView;
00142 #endif
00143 
00144   QLabel *label = new QLabel( i18nc( "@label Search in a list of contacts", "Search:" ) );
00145   mSearchLine = new SearchLineEdit( mView );
00146   label->setBuddy( mSearchLine );
00147   searchLayout->addWidget( label );
00148   searchLayout->addWidget( mSearchLine );
00149 
00150 #ifndef QT_NO_DRAGANDDROP
00151   mView->setDragDropMode( QAbstractItemView::NoDragDrop );
00152 #endif
00153   layout->addWidget( mView );
00154 
00155   Akonadi::ContactsFilterProxyModel *filter = new Akonadi::ContactsFilterProxyModel( q );
00156   filter->setSourceModel( mModel );
00157 
00158   mSelectionModel = new EmailAddressSelectionProxyModel( q );
00159   mSelectionModel->setSourceModel( filter );
00160 
00161   mView->setModel( mSelectionModel );
00162   mView->header()->hide();
00163 
00164   q->connect( mSearchLine, SIGNAL( textChanged( const QString& ) ),
00165               filter, SLOT( setFilterString( const QString& ) ) );
00166 
00167   Control::widgetNeedsAkonadi( q );
00168 
00169   mSearchLine->setFocus();
00170 
00171   QTimer::singleShot( 1000, mView, SLOT( expandAll() ) );
00172 }
00173 
00174 
00175 EmailAddressSelectionWidget::EmailAddressSelectionWidget( QWidget * parent )
00176   : QWidget( parent ),
00177     d( new Private( this, 0 ) )
00178 {
00179 }
00180 
00181 EmailAddressSelectionWidget::EmailAddressSelectionWidget( QAbstractItemModel *model, QWidget * parent )
00182   : QWidget( parent ),
00183     d( new Private( this, model ) )
00184 {
00185 }
00186 
00187 EmailAddressSelectionWidget::~EmailAddressSelectionWidget()
00188 {
00189   delete d;
00190 }
00191 
00192 EmailAddressSelection::List EmailAddressSelectionWidget::selectedAddresses() const
00193 {
00194   EmailAddressSelection::List selections;
00195 
00196   if ( !d->mView->selectionModel() )
00197     return selections;
00198 
00199   const QModelIndexList selectedRows = d->mView->selectionModel()->selectedRows( 0 );
00200   foreach ( const QModelIndex &index, selectedRows ) {
00201     EmailAddressSelection selection;
00202     selection.d->mName = index.data( EmailAddressSelectionProxyModel::NameRole ).toString();
00203     selection.d->mEmailAddress = index.data( EmailAddressSelectionProxyModel::EmailAddressRole ).toString();
00204     selection.d->mItem = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>();
00205 
00206     if ( !selection.d->mEmailAddress.isEmpty() )
00207       selections << selection;
00208   }
00209 
00210   return selections;
00211 }
00212 
00213 KLineEdit* EmailAddressSelectionWidget::searchLineEdit() const
00214 {
00215   return d->mSearchLine;
00216 }
00217 
00218 QTreeView* EmailAddressSelectionWidget::view() const
00219 {
00220   return d->mView;
00221 }
00222 
00223 #include "emailaddressselectionwidget.moc"

akonadi/contact

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

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  •   contact
  •   kmime
  • kabc
  • 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
Generated for KDE-PIM Libraries by doxygen 1.7.5
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal