akonadi
collectioncombobox.cpp
00001 /* 00002 This file is part of Akonadi Contact. 00003 00004 Copyright (c) 2007-2009 Tobias Koenig <tokoe@kde.org> 00005 00006 This library is free software; you can redistribute it and/or modify it 00007 under the terms of the GNU Library General Public License as published by 00008 the Free Software Foundation; either version 2 of the License, or (at your 00009 option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, but WITHOUT 00012 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00013 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00014 License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to the 00018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00019 02110-1301, USA. 00020 */ 00021 00022 #include "collectioncombobox.h" 00023 00024 #include "asyncselectionhandler_p.h" 00025 00026 #include <akonadi/changerecorder.h> 00027 #include <akonadi/collectionfetchscope.h> 00028 #include <akonadi/collectionfilterproxymodel.h> 00029 #include <akonadi/entityrightsfiltermodel.h> 00030 #include <akonadi/entitytreemodel.h> 00031 #include <akonadi/session.h> 00032 00033 #include "kdescendantsproxymodel_p.h" 00034 #include "collectionutils_p.h" 00035 00036 #include <QtCore/QAbstractItemModel> 00037 00038 using namespace Akonadi; 00039 00040 class CollectionComboBox::Private 00041 { 00042 public: 00043 Private( QAbstractItemModel *customModel, CollectionComboBox *parent ) 00044 : mParent( parent ), mMonitor( 0 ), mModel( 0 ) 00045 { 00046 QAbstractItemModel *baseModel; 00047 00048 if ( customModel ) { 00049 baseModel = customModel; 00050 } else { 00051 mMonitor = new Akonadi::ChangeRecorder( mParent ); 00052 mMonitor->fetchCollection( true ); 00053 mMonitor->setCollectionMonitored( Akonadi::Collection::root() ); 00054 00055 mModel = new EntityTreeModel( mMonitor, mParent ); 00056 mModel->setItemPopulationStrategy( EntityTreeModel::NoItemPopulation ); 00057 00058 baseModel = mModel; 00059 } 00060 00061 KDescendantsProxyModel *proxyModel = new KDescendantsProxyModel( parent ); 00062 proxyModel->setDisplayAncestorData( true ); 00063 proxyModel->setSourceModel( baseModel ); 00064 00065 mMimeTypeFilterModel = new CollectionFilterProxyModel( parent ); 00066 mMimeTypeFilterModel->setSourceModel( proxyModel ); 00067 00068 mRightsFilterModel = new EntityRightsFilterModel( parent ); 00069 mRightsFilterModel->setSourceModel( mMimeTypeFilterModel ); 00070 00071 mParent->setModel( mRightsFilterModel ); 00072 00073 mSelectionHandler = new AsyncSelectionHandler( mRightsFilterModel, mParent ); 00074 mParent->connect( mSelectionHandler, SIGNAL( collectionAvailable( const QModelIndex& ) ), 00075 mParent, SLOT( activated( const QModelIndex& ) ) ); 00076 00077 mParent->connect( mParent, SIGNAL( activated( int ) ), 00078 mParent, SLOT( activated( int ) ) ); 00079 } 00080 00081 ~Private() 00082 { 00083 } 00084 00085 void activated( int index ); 00086 void activated( const QModelIndex& index ); 00087 00088 CollectionComboBox *mParent; 00089 00090 ChangeRecorder *mMonitor; 00091 EntityTreeModel *mModel; 00092 CollectionFilterProxyModel *mMimeTypeFilterModel; 00093 EntityRightsFilterModel *mRightsFilterModel; 00094 AsyncSelectionHandler *mSelectionHandler; 00095 }; 00096 00097 void CollectionComboBox::Private::activated( int index ) 00098 { 00099 const QModelIndex modelIndex = mParent->model()->index( index, 0 ); 00100 if ( modelIndex.isValid() ) 00101 emit mParent->currentChanged( modelIndex.data( EntityTreeModel::CollectionRole).value<Collection>() ); 00102 } 00103 00104 void CollectionComboBox::Private::activated( const QModelIndex &index ) 00105 { 00106 mParent->setCurrentIndex( index.row() ); 00107 } 00108 00109 00110 CollectionComboBox::CollectionComboBox( QWidget *parent ) 00111 : KComboBox( parent ), d( new Private( 0, this ) ) 00112 { 00113 } 00114 00115 CollectionComboBox::CollectionComboBox( QAbstractItemModel *model, QWidget *parent ) 00116 : KComboBox( parent ), d( new Private( model, this ) ) 00117 { 00118 } 00119 00120 CollectionComboBox::~CollectionComboBox() 00121 { 00122 delete d; 00123 } 00124 00125 void CollectionComboBox::setMimeTypeFilter( const QStringList &contentMimeTypes ) 00126 { 00127 d->mMimeTypeFilterModel->clearFilters(); 00128 d->mMimeTypeFilterModel->addMimeTypeFilters( contentMimeTypes ); 00129 00130 if ( d->mMonitor ) 00131 foreach ( const QString &mimeType, contentMimeTypes ) 00132 d->mMonitor->setMimeTypeMonitored( mimeType, true ); 00133 } 00134 00135 QStringList CollectionComboBox::mimeTypeFilter() const 00136 { 00137 return d->mMimeTypeFilterModel->mimeTypeFilters(); 00138 } 00139 00140 void CollectionComboBox::setAccessRightsFilter( Collection::Rights rights ) 00141 { 00142 d->mRightsFilterModel->setAccessRights( rights ); 00143 } 00144 00145 Akonadi::Collection::Rights CollectionComboBox::accessRightsFilter() const 00146 { 00147 return d->mRightsFilterModel->accessRights(); 00148 } 00149 00150 void CollectionComboBox::setDefaultCollection( const Collection &collection ) 00151 { 00152 d->mSelectionHandler->waitForCollection( collection ); 00153 } 00154 00155 Akonadi::Collection CollectionComboBox::currentCollection() const 00156 { 00157 const QModelIndex modelIndex = model()->index( currentIndex(), 0 ); 00158 if ( modelIndex.isValid() ) 00159 return modelIndex.data( Akonadi::EntityTreeModel::CollectionRole ).value<Collection>(); 00160 else 00161 return Akonadi::Collection(); 00162 } 00163 00164 #include "collectioncombobox.moc"