00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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 Akonadi::EntityTreeView *mView;
00093 EmailAddressSelectionProxyModel *mSelectionModel;
00094 };
00095
00096 void EmailAddressSelectionWidget::Private::init()
00097 {
00098
00099 if ( !mModel ) {
00100 Akonadi::Session *session = new Akonadi::Session( "InternalEmailAddressSelectionWidgetModel", q );
00101
00102 Akonadi::ItemFetchScope scope;
00103 scope.fetchFullPayload( true );
00104 scope.fetchAttribute<Akonadi::EntityDisplayAttribute>();
00105
00106 Akonadi::ChangeRecorder *changeRecorder = new Akonadi::ChangeRecorder( q );
00107 changeRecorder->setSession( session );
00108 changeRecorder->fetchCollection( true );
00109 changeRecorder->setItemFetchScope( scope );
00110 changeRecorder->setCollectionMonitored( Akonadi::Collection::root() );
00111 changeRecorder->setMimeTypeMonitored( KABC::Addressee::mimeType(), true );
00112 changeRecorder->setMimeTypeMonitored( KABC::ContactGroup::mimeType(), true );
00113
00114 Akonadi::ContactsTreeModel *model = new Akonadi::ContactsTreeModel( changeRecorder, q );
00115
00116
00117 mModel = model;
00118 }
00119
00120
00121 QVBoxLayout *layout = new QVBoxLayout( q );
00122
00123 mDescriptionLabel = new QLabel;
00124 mDescriptionLabel->hide();
00125 layout->addWidget( mDescriptionLabel );
00126
00127 QHBoxLayout *searchLayout = new QHBoxLayout;
00128 layout->addLayout( searchLayout );
00129
00130 mView = new Akonadi::EntityTreeView;
00131
00132 QLabel *label = new QLabel( i18nc( "@label Search in a list of contacts", "Search:" ) );
00133 mSearchLine = new SearchLineEdit( mView );
00134 label->setBuddy( mSearchLine );
00135 searchLayout->addWidget( label );
00136 searchLayout->addWidget( mSearchLine );
00137
00138 mView->setDragDropMode( QAbstractItemView::NoDragDrop );
00139 layout->addWidget( mView );
00140
00141 Akonadi::ContactsFilterProxyModel *filter = new Akonadi::ContactsFilterProxyModel( q );
00142 filter->setSourceModel( mModel );
00143
00144 mSelectionModel = new EmailAddressSelectionProxyModel( q );
00145 mSelectionModel->setSourceModel( filter );
00146
00147 mView->setModel( mSelectionModel );
00148 mView->header()->hide();
00149
00150 q->connect( mSearchLine, SIGNAL( textChanged( const QString& ) ),
00151 filter, SLOT( setFilterString( const QString& ) ) );
00152
00153 Control::widgetNeedsAkonadi( q );
00154
00155 mSearchLine->setFocus();
00156
00157 QTimer::singleShot( 1000, mView, SLOT( expandAll() ) );
00158 }
00159
00160
00161 EmailAddressSelectionWidget::EmailAddressSelectionWidget( QWidget * parent )
00162 : QWidget( parent ),
00163 d( new Private( this, 0 ) )
00164 {
00165 }
00166
00167 EmailAddressSelectionWidget::EmailAddressSelectionWidget( QAbstractItemModel *model, QWidget * parent )
00168 : QWidget( parent ),
00169 d( new Private( this, model ) )
00170 {
00171 }
00172
00173 EmailAddressSelectionWidget::~EmailAddressSelectionWidget()
00174 {
00175 delete d;
00176 }
00177
00178 EmailAddressSelection::List EmailAddressSelectionWidget::selectedAddresses() const
00179 {
00180 EmailAddressSelection::List selections;
00181
00182 if ( !d->mView->selectionModel() )
00183 return selections;
00184
00185 const QModelIndexList selectedRows = d->mView->selectionModel()->selectedRows( 0 );
00186 foreach ( const QModelIndex &index, selectedRows ) {
00187 EmailAddressSelection selection;
00188 selection.d->mName = index.data( EmailAddressSelectionProxyModel::NameRole ).toString();
00189 selection.d->mEmailAddress = index.data( EmailAddressSelectionProxyModel::EmailAddressRole ).toString();
00190 selection.d->mItem = index.data( ContactsTreeModel::ItemRole ).value<Akonadi::Item>();
00191
00192 if ( !selection.d->mEmailAddress.isEmpty() )
00193 selections << selection;
00194 }
00195
00196 return selections;
00197 }
00198
00199 KLineEdit* EmailAddressSelectionWidget::searchLineEdit() const
00200 {
00201 return d->mSearchLine;
00202 }
00203
00204 QTreeView* EmailAddressSelectionWidget::view() const
00205 {
00206 return d->mView;
00207 }
00208
00209 #include "emailaddressselectionwidget.moc"