akonadi
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 00041 #include <QtCore/QTimer> 00042 #include <QtGui/QHBoxLayout> 00043 #include <QtGui/QHeaderView> 00044 #include <QtGui/QKeyEvent> 00045 #include <QtGui/QLabel> 00046 #include <QtGui/QVBoxLayout> 00047 00048 using namespace Akonadi; 00049 00053 class SearchLineEdit : public KLineEdit 00054 { 00055 public: 00056 SearchLineEdit( QWidget *receiver, QWidget *parent = 0 ) 00057 : KLineEdit( parent ), mReceiver( receiver ) 00058 { 00059 } 00060 00061 protected: 00062 virtual void keyPressEvent( QKeyEvent *event ) 00063 { 00064 if ( event->key() == Qt::Key_Down ) 00065 QMetaObject::invokeMethod( mReceiver, "setFocus" ); 00066 00067 KLineEdit::keyPressEvent( event ); 00068 } 00069 00070 private: 00071 QWidget *mReceiver; 00072 }; 00073 00077 class EmailAddressSelectionWidget::Private 00078 { 00079 public: 00080 Private( EmailAddressSelectionWidget *qq, QAbstractItemModel *model ) 00081 : q( qq ), mModel( model ) 00082 { 00083 init(); 00084 } 00085 00086 void init(); 00087 00088 EmailAddressSelectionWidget *q; 00089 QAbstractItemModel *mModel; 00090 QLabel *mDescriptionLabel; 00091 SearchLineEdit *mSearchLine; 00092 // FIXME: Temporary until EntityTreeView compiles 00093 #ifndef Q_OS_WINCE 00094 Akonadi::EntityTreeView *mView; 00095 #else 00096 QTreeView* mView; 00097 #endif 00098 EmailAddressSelectionProxyModel *mSelectionModel; 00099 }; 00100 00101 void EmailAddressSelectionWidget::Private::init() 00102 { 00103 // setup internal model if needed 00104 if ( !mModel ) { 00105 Akonadi::Session *session = new Akonadi::Session( "InternalEmailAddressSelectionWidgetModel", q ); 00106 00107 Akonadi::ItemFetchScope scope; 00108 scope.fetchFullPayload( true ); 00109 scope.fetchAttribute<Akonadi::EntityDisplayAttribute>(); 00110 00111 Akonadi::ChangeRecorder *changeRecorder = new Akonadi::ChangeRecorder( q ); 00112 changeRecorder->setSession( session ); 00113 changeRecorder->fetchCollection( true ); 00114 changeRecorder->setItemFetchScope( scope ); 00115 changeRecorder->setCollectionMonitored( Akonadi::Collection::root() ); 00116 changeRecorder->setMimeTypeMonitored( KABC::Addressee::mimeType(), true ); 00117 changeRecorder->setMimeTypeMonitored( KABC::ContactGroup::mimeType(), true ); 00118 00119 Akonadi::ContactsTreeModel *model = new Akonadi::ContactsTreeModel( changeRecorder, q ); 00120 // model->setCollectionFetchStrategy( Akonadi::ContactsTreeModel::InvisibleFetch ); 00121 00122 mModel = model; 00123 } 00124 00125 // setup ui 00126 QVBoxLayout *layout = new QVBoxLayout( q ); 00127 00128 mDescriptionLabel = new QLabel; 00129 mDescriptionLabel->hide(); 00130 layout->addWidget( mDescriptionLabel ); 00131 00132 QHBoxLayout *searchLayout = new QHBoxLayout; 00133 layout->addLayout( searchLayout ); 00134 00135 // FIXME: Temporary until EntityTreeView compiles 00136 #ifndef Q_OS_WINCE 00137 mView = new Akonadi::EntityTreeView; 00138 #else 00139 mView = new QTreeView; 00140 #endif 00141 00142 QLabel *label = new QLabel( i18nc( "@label Search in a list of contacts", "Search:" ) ); 00143 mSearchLine = new SearchLineEdit( mView ); 00144 label->setBuddy( mSearchLine ); 00145 searchLayout->addWidget( label ); 00146 searchLayout->addWidget( mSearchLine ); 00147 00148 #ifndef QT_NO_DRAGANDDROP 00149 mView->setDragDropMode( QAbstractItemView::NoDragDrop ); 00150 #endif 00151 layout->addWidget( mView ); 00152 00153 Akonadi::ContactsFilterProxyModel *filter = new Akonadi::ContactsFilterProxyModel( q ); 00154 filter->setSourceModel( mModel ); 00155 00156 mSelectionModel = new EmailAddressSelectionProxyModel( q ); 00157 mSelectionModel->setSourceModel( filter ); 00158 00159 mView->setModel( mSelectionModel ); 00160 mView->header()->hide(); 00161 00162 q->connect( mSearchLine, SIGNAL( textChanged( const QString& ) ), 00163 filter, SLOT( setFilterString( const QString& ) ) ); 00164 00165 Control::widgetNeedsAkonadi( q ); 00166 00167 mSearchLine->setFocus(); 00168 00169 QTimer::singleShot( 1000, mView, SLOT( expandAll() ) ); 00170 } 00171 00172 00173 EmailAddressSelectionWidget::EmailAddressSelectionWidget( QWidget * parent ) 00174 : QWidget( parent ), 00175 d( new Private( this, 0 ) ) 00176 { 00177 } 00178 00179 EmailAddressSelectionWidget::EmailAddressSelectionWidget( QAbstractItemModel *model, QWidget * parent ) 00180 : QWidget( parent ), 00181 d( new Private( this, model ) ) 00182 { 00183 } 00184 00185 EmailAddressSelectionWidget::~EmailAddressSelectionWidget() 00186 { 00187 delete d; 00188 } 00189 00190 EmailAddressSelection::List EmailAddressSelectionWidget::selectedAddresses() const 00191 { 00192 EmailAddressSelection::List selections; 00193 00194 if ( !d->mView->selectionModel() ) 00195 return selections; 00196 00197 const QModelIndexList selectedRows = d->mView->selectionModel()->selectedRows( 0 ); 00198 foreach ( const QModelIndex &index, selectedRows ) { 00199 EmailAddressSelection selection; 00200 selection.d->mName = index.data( EmailAddressSelectionProxyModel::NameRole ).toString(); 00201 selection.d->mEmailAddress = index.data( EmailAddressSelectionProxyModel::EmailAddressRole ).toString(); 00202 selection.d->mItem = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>(); 00203 00204 if ( !selection.d->mEmailAddress.isEmpty() ) 00205 selections << selection; 00206 } 00207 00208 return selections; 00209 } 00210 00211 KLineEdit* EmailAddressSelectionWidget::searchLineEdit() const 00212 { 00213 return d->mSearchLine; 00214 } 00215 00216 QTreeView* EmailAddressSelectionWidget::view() const 00217 { 00218 return d->mView; 00219 } 00220 00221 #include "emailaddressselectionwidget.moc"