akonadi
collectiondialog.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "collectiondialog.h"
00021
00022 #include "asyncselectionhandler_p.h"
00023
00024 #include <akonadi/changerecorder.h>
00025 #include <akonadi/collectionfetchscope.h>
00026 #include <akonadi/collectionfilterproxymodel.h>
00027 #include <akonadi/entityrightsfiltermodel.h>
00028 #include <akonadi/entitytreemodel.h>
00029 #include <akonadi/entitytreeview.h>
00030 #include <akonadi/session.h>
00031
00032 #include <QtGui/QHeaderView>
00033 #include <QtGui/QLabel>
00034 #include <QtGui/QVBoxLayout>
00035
00036 using namespace Akonadi;
00037
00038 class CollectionDialog::Private
00039 {
00040 public:
00041 Private( QAbstractItemModel *customModel, CollectionDialog *parent )
00042 : mParent( parent ),
00043 mMonitor( 0 ),
00044 mModel( 0 )
00045 {
00046
00047 QWidget *widget = mParent->mainWidget();
00048 QVBoxLayout *layout = new QVBoxLayout( widget );
00049
00050 mTextLabel = new QLabel;
00051 layout->addWidget( mTextLabel );
00052 mTextLabel->hide();
00053
00054 mView = new EntityTreeView;
00055 mView->header()->hide();
00056 layout->addWidget( mView );
00057
00058
00059 mParent->enableButton( KDialog::Ok, false );
00060
00061
00062 QAbstractItemModel *baseModel;
00063
00064 if ( customModel ) {
00065 baseModel = customModel;
00066 } else {
00067 mMonitor = new Akonadi::ChangeRecorder( mParent );
00068 mMonitor->fetchCollection( true );
00069 mMonitor->setCollectionMonitored( Akonadi::Collection::root() );
00070
00071 mModel = new EntityTreeModel( mMonitor, mParent );
00072 mModel->setItemPopulationStrategy( EntityTreeModel::NoItemPopulation );
00073 baseModel = mModel;
00074 }
00075
00076 mMimeTypeFilterModel = new CollectionFilterProxyModel( mParent );
00077 mMimeTypeFilterModel->setSourceModel( baseModel );
00078
00079 mRightsFilterModel = new EntityRightsFilterModel( mParent );
00080 mRightsFilterModel->setSourceModel( mMimeTypeFilterModel );
00081
00082 mSelectionHandler = new AsyncSelectionHandler( mRightsFilterModel, mParent );
00083 mParent->connect( mSelectionHandler, SIGNAL( collectionAvailable( const QModelIndex& ) ),
00084 mParent, SLOT( slotCollectionAvailable( const QModelIndex& ) ) );
00085 mView->setModel( mRightsFilterModel );
00086
00087 mParent->connect( mView->selectionModel(), SIGNAL( selectionChanged( QItemSelection, QItemSelection ) ),
00088 mParent, SLOT( slotSelectionChanged() ) );
00089 }
00090
00091 ~Private()
00092 {
00093 }
00094
00095 void slotCollectionAvailable( const QModelIndex &index )
00096 {
00097 mView->expandAll();
00098 mView->setCurrentIndex( index );
00099 }
00100
00101 CollectionDialog *mParent;
00102
00103 ChangeRecorder *mMonitor;
00104 EntityTreeModel *mModel;
00105 CollectionFilterProxyModel *mMimeTypeFilterModel;
00106 EntityRightsFilterModel *mRightsFilterModel;
00107 EntityTreeView *mView;
00108 AsyncSelectionHandler *mSelectionHandler;
00109 QLabel *mTextLabel;
00110
00111 void slotSelectionChanged();
00112 };
00113
00114 void CollectionDialog::Private::slotSelectionChanged()
00115 {
00116 mParent->enableButton( KDialog::Ok, mView->selectionModel()->selectedIndexes().count() > 0 );
00117 }
00118
00119 CollectionDialog::CollectionDialog( QWidget *parent )
00120 : KDialog( parent ),
00121 d( new Private( 0, this ) )
00122 {
00123 }
00124
00125 CollectionDialog::CollectionDialog( QAbstractItemModel *model, QWidget *parent )
00126 : KDialog( parent ),
00127 d( new Private( model, this ) )
00128 {
00129 }
00130
00131 CollectionDialog::~CollectionDialog()
00132 {
00133 delete d;
00134 }
00135
00136 Akonadi::Collection CollectionDialog::selectedCollection() const
00137 {
00138 if ( selectionMode() == QAbstractItemView::SingleSelection ) {
00139 const QModelIndex index = d->mView->currentIndex();
00140 if ( index.isValid() )
00141 return index.model()->data( index, EntityTreeModel::CollectionRole ).value<Collection>();
00142 }
00143
00144 return Collection();
00145 }
00146
00147 Akonadi::Collection::List CollectionDialog::selectedCollections() const
00148 {
00149 Collection::List collections;
00150 const QItemSelectionModel *selectionModel = d->mView->selectionModel();
00151 const QModelIndexList selectedIndexes = selectionModel->selectedIndexes();
00152 foreach ( const QModelIndex &index, selectedIndexes ) {
00153 if ( index.isValid() ) {
00154 const Collection collection = index.model()->data( index, EntityTreeModel::CollectionRole ).value<Collection>();
00155 if ( collection.isValid() )
00156 collections.append( collection );
00157 }
00158 }
00159
00160 return collections;
00161 }
00162
00163 void CollectionDialog::setMimeTypeFilter( const QStringList &mimeTypes )
00164 {
00165 d->mMimeTypeFilterModel->clearFilters();
00166 d->mMimeTypeFilterModel->addMimeTypeFilters( mimeTypes );
00167
00168 if ( d->mMonitor )
00169 foreach( const QString &mimetype, mimeTypes )
00170 d->mMonitor->setMimeTypeMonitored( mimetype );
00171 }
00172
00173 QStringList CollectionDialog::mimeTypeFilter() const
00174 {
00175 return d->mMimeTypeFilterModel->mimeTypeFilters();
00176 }
00177
00178 void CollectionDialog::setAccessRightsFilter( Collection::Rights rights )
00179 {
00180 d->mRightsFilterModel->setAccessRights( rights );
00181 }
00182
00183 Collection::Rights CollectionDialog::accessRightsFilter() const
00184 {
00185 return d->mRightsFilterModel->accessRights();
00186 }
00187
00188 void CollectionDialog::setDescription( const QString &text )
00189 {
00190 d->mTextLabel->setText( text );
00191 d->mTextLabel->show();
00192 }
00193
00194 void CollectionDialog::setDefaultCollection( const Collection &collection )
00195 {
00196 d->mSelectionHandler->waitForCollection( collection );
00197 }
00198
00199 void CollectionDialog::setSelectionMode( QAbstractItemView::SelectionMode mode )
00200 {
00201 d->mView->setSelectionMode( mode );
00202 }
00203
00204 QAbstractItemView::SelectionMode CollectionDialog::selectionMode() const
00205 {
00206 return d->mView->selectionMode();
00207 }
00208
00209 #include "collectiondialog.moc"