akonadi
collectiondialog_desktop.cpp
00001 /* 00002 Copyright 2008 Ingo Klöcker <kloecker@kde.org> 00003 Copyright 2010 Laurent Montel <montel@kde.org> 00004 00005 This library is free software; you can redistribute it and/or modify it 00006 under the terms of the GNU Library General Public License as published by 00007 the Free Software Foundation; either version 2 of the License, or (at your 00008 option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, but WITHOUT 00011 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00012 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00013 License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to the 00017 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00018 02110-1301, USA. 00019 */ 00020 00021 #include "collectiondialog.h" 00022 00023 #include "asyncselectionhandler_p.h" 00024 00025 #include <akonadi/changerecorder.h> 00026 #include <akonadi/collectionfetchscope.h> 00027 #include <akonadi/collectionfilterproxymodel.h> 00028 #include <akonadi/entityrightsfiltermodel.h> 00029 #include <akonadi/entitytreemodel.h> 00030 #include <akonadi/entitytreeview.h> 00031 #include <akonadi/session.h> 00032 #include <akonadi/collectioncreatejob.h> 00033 #include <akonadi/collectionutils_p.h> 00034 00035 #include <QtGui/QHeaderView> 00036 #include <QtGui/QLabel> 00037 #include <QtGui/QVBoxLayout> 00038 00039 #include <KLineEdit> 00040 #include <KLocale> 00041 #include <KInputDialog> 00042 #include <KMessageBox> 00043 00044 using namespace Akonadi; 00045 00046 class CollectionDialog::Private 00047 { 00048 public: 00049 Private( QAbstractItemModel *customModel, CollectionDialog *parent, CollectionDialogOptions options ) 00050 : mParent( parent ), 00051 mMonitor( 0 ) 00052 { 00053 // setup GUI 00054 QWidget *widget = mParent->mainWidget(); 00055 QVBoxLayout *layout = new QVBoxLayout( widget ); 00056 00057 changeCollectionDialogOptions( options ); 00058 00059 mTextLabel = new QLabel; 00060 layout->addWidget( mTextLabel ); 00061 mTextLabel->hide(); 00062 00063 KLineEdit* filterCollectionLineEdit = new KLineEdit( widget ); 00064 filterCollectionLineEdit->setClearButtonShown( true ); 00065 filterCollectionLineEdit->setClickMessage( i18nc( "@info/plain Displayed grayed-out inside the " 00066 "textbox, verb to search", "Search" ) ); 00067 layout->addWidget( filterCollectionLineEdit ); 00068 00069 mView = new EntityTreeView; 00070 mView->setDragDropMode( QAbstractItemView::NoDragDrop ); 00071 mView->header()->hide(); 00072 layout->addWidget( mView ); 00073 00074 00075 mParent->enableButton( KDialog::Ok, false ); 00076 00077 // setup models 00078 QAbstractItemModel *baseModel; 00079 00080 if ( customModel ) { 00081 baseModel = customModel; 00082 } else { 00083 mMonitor = new Akonadi::ChangeRecorder( mParent ); 00084 mMonitor->fetchCollection( true ); 00085 mMonitor->setCollectionMonitored( Akonadi::Collection::root() ); 00086 00087 EntityTreeModel *model = new EntityTreeModel( mMonitor, mParent ); 00088 model->setItemPopulationStrategy( EntityTreeModel::NoItemPopulation ); 00089 baseModel = model; 00090 } 00091 00092 mMimeTypeFilterModel = new CollectionFilterProxyModel( mParent ); 00093 mMimeTypeFilterModel->setSourceModel( baseModel ); 00094 mMimeTypeFilterModel->setExcludeVirtualCollections( true ); 00095 00096 mRightsFilterModel = new EntityRightsFilterModel( mParent ); 00097 mRightsFilterModel->setSourceModel( mMimeTypeFilterModel ); 00098 00099 mSelectionHandler = new AsyncSelectionHandler( mRightsFilterModel, mParent ); 00100 mParent->connect( mSelectionHandler, SIGNAL(collectionAvailable(QModelIndex)), 00101 mParent, SLOT(slotCollectionAvailable(QModelIndex)) ); 00102 00103 KRecursiveFilterProxyModel* filterCollection = new KRecursiveFilterProxyModel( mParent ); 00104 filterCollection->setDynamicSortFilter( true ); 00105 filterCollection->setSourceModel( mRightsFilterModel ); 00106 filterCollection->setFilterCaseSensitivity( Qt::CaseInsensitive ); 00107 mView->setModel( filterCollection ); 00108 00109 mParent->connect( filterCollectionLineEdit, SIGNAL(textChanged(QString)), 00110 filterCollection, SLOT(setFilterFixedString(QString)) ); 00111 00112 mParent->connect( mView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), 00113 mParent, SLOT(slotSelectionChanged()) ); 00114 00115 mParent->connect( mView, SIGNAL(doubleClicked(QModelIndex)), 00116 mParent, SLOT(accept()) ); 00117 00118 } 00119 00120 ~Private() 00121 { 00122 } 00123 00124 void slotCollectionAvailable( const QModelIndex &index ) 00125 { 00126 mView->expandAll(); 00127 mView->setCurrentIndex( index ); 00128 } 00129 00130 CollectionDialog *mParent; 00131 00132 ChangeRecorder *mMonitor; 00133 CollectionFilterProxyModel *mMimeTypeFilterModel; 00134 EntityRightsFilterModel *mRightsFilterModel; 00135 EntityTreeView *mView; 00136 AsyncSelectionHandler *mSelectionHandler; 00137 QLabel *mTextLabel; 00138 bool mAllowToCreateNewChildCollection; 00139 00140 void slotSelectionChanged(); 00141 void slotAddChildCollection(); 00142 void slotCollectionCreationResult( KJob* job ); 00143 bool canCreateCollection( const Akonadi::Collection &parentCollection ) const; 00144 void changeCollectionDialogOptions( CollectionDialogOptions options ); 00145 00146 }; 00147 00148 void CollectionDialog::Private::slotSelectionChanged() 00149 { 00150 mParent->enableButton( KDialog::Ok, mView->selectionModel()->selectedIndexes().count() > 0 ); 00151 if ( mAllowToCreateNewChildCollection ) { 00152 const Akonadi::Collection parentCollection = mParent->selectedCollection(); 00153 const bool canCreateChildCollections = canCreateCollection( parentCollection ); 00154 const bool isVirtual = Akonadi::CollectionUtils::isVirtual( parentCollection ); 00155 00156 mParent->enableButton( KDialog::User1, (canCreateChildCollections && !isVirtual) ); 00157 if ( parentCollection.isValid() ) { 00158 const bool canCreateItems = (parentCollection.rights() & Akonadi::Collection::CanCreateItem); 00159 mParent->enableButton( KDialog::Ok, canCreateItems ); 00160 } 00161 } 00162 } 00163 00164 void CollectionDialog::Private::changeCollectionDialogOptions( CollectionDialogOptions options ) 00165 { 00166 mAllowToCreateNewChildCollection = ( options & AllowToCreateNewChildCollection ); 00167 if ( mAllowToCreateNewChildCollection ) { 00168 mParent->setButtons( Ok | Cancel | User1 ); 00169 mParent->setButtonGuiItem( User1, KGuiItem( i18n( "&New Subfolder..." ), QLatin1String( "folder-new" ), 00170 i18n( "Create a new subfolder under the currently selected folder" ) ) ); 00171 mParent->enableButton( KDialog::User1, false ); 00172 connect( mParent, SIGNAL(user1Clicked()), mParent, SLOT(slotAddChildCollection()) ); 00173 } 00174 } 00175 00176 00177 00178 bool CollectionDialog::Private::canCreateCollection( const Akonadi::Collection &parentCollection ) const 00179 { 00180 if ( !parentCollection.isValid() ) 00181 return false; 00182 00183 if ( ( parentCollection.rights() & Akonadi::Collection::CanCreateCollection ) ) { 00184 const QStringList dialogMimeTypeFilter = mParent->mimeTypeFilter(); 00185 const QStringList parentCollectionMimeTypes = parentCollection.contentMimeTypes(); 00186 Q_FOREACH ( const QString& mimetype, dialogMimeTypeFilter ) { 00187 if ( parentCollectionMimeTypes.contains( mimetype ) ) 00188 return true; 00189 } 00190 return true; 00191 } 00192 return false; 00193 } 00194 00195 00196 void CollectionDialog::Private::slotAddChildCollection() 00197 { 00198 const Akonadi::Collection parentCollection = mParent->selectedCollection(); 00199 if ( canCreateCollection( parentCollection ) ) { 00200 const QString name = KInputDialog::getText( i18nc( "@title:window", "New Folder" ), 00201 i18nc( "@label:textbox, name of a thing", "Name" ), 00202 QString(), 0, mParent ); 00203 if ( name.isEmpty() ) 00204 return; 00205 00206 Akonadi::Collection collection; 00207 collection.setName( name ); 00208 collection.setParentCollection( parentCollection ); 00209 Akonadi::CollectionCreateJob *job = new Akonadi::CollectionCreateJob( collection ); 00210 connect( job, SIGNAL(result(KJob*)), mParent, SLOT(slotCollectionCreationResult(KJob*)) ); 00211 } 00212 } 00213 00214 void CollectionDialog::Private::slotCollectionCreationResult( KJob* job ) 00215 { 00216 if ( job->error() ) { 00217 KMessageBox::error( mParent, i18n( "Could not create folder: %1", job->errorString() ), 00218 i18n( "Folder creation failed" ) ); 00219 } 00220 } 00221 00222 00223 00224 CollectionDialog::CollectionDialog( QWidget *parent ) 00225 : KDialog( parent ), 00226 d( new Private( 0, this, CollectionDialog::None ) ) 00227 { 00228 } 00229 00230 CollectionDialog::CollectionDialog( QAbstractItemModel *model, QWidget *parent ) 00231 : KDialog( parent ), 00232 d( new Private( model, this, CollectionDialog::None ) ) 00233 { 00234 } 00235 00236 CollectionDialog::CollectionDialog( CollectionDialogOptions options, QAbstractItemModel *model, QWidget *parent ) 00237 : KDialog( parent ), 00238 d( new Private( model, this, options ) ) 00239 { 00240 } 00241 00242 00243 CollectionDialog::~CollectionDialog() 00244 { 00245 delete d; 00246 } 00247 00248 Akonadi::Collection CollectionDialog::selectedCollection() const 00249 { 00250 if ( selectionMode() == QAbstractItemView::SingleSelection ) { 00251 const QModelIndex index = d->mView->currentIndex(); 00252 if ( index.isValid() ) 00253 return index.model()->data( index, EntityTreeModel::CollectionRole ).value<Collection>(); 00254 } 00255 00256 return Collection(); 00257 } 00258 00259 Akonadi::Collection::List CollectionDialog::selectedCollections() const 00260 { 00261 Collection::List collections; 00262 const QItemSelectionModel *selectionModel = d->mView->selectionModel(); 00263 const QModelIndexList selectedIndexes = selectionModel->selectedIndexes(); 00264 foreach ( const QModelIndex &index, selectedIndexes ) { 00265 if ( index.isValid() ) { 00266 const Collection collection = index.model()->data( index, EntityTreeModel::CollectionRole ).value<Collection>(); 00267 if ( collection.isValid() ) 00268 collections.append( collection ); 00269 } 00270 } 00271 00272 return collections; 00273 } 00274 00275 void CollectionDialog::setMimeTypeFilter( const QStringList &mimeTypes ) 00276 { 00277 d->mMimeTypeFilterModel->clearFilters(); 00278 d->mMimeTypeFilterModel->addMimeTypeFilters( mimeTypes ); 00279 00280 if ( d->mMonitor ) 00281 foreach ( const QString &mimetype, mimeTypes ) 00282 d->mMonitor->setMimeTypeMonitored( mimetype ); 00283 } 00284 00285 QStringList CollectionDialog::mimeTypeFilter() const 00286 { 00287 return d->mMimeTypeFilterModel->mimeTypeFilters(); 00288 } 00289 00290 void CollectionDialog::setAccessRightsFilter( Collection::Rights rights ) 00291 { 00292 d->mRightsFilterModel->setAccessRights( rights ); 00293 } 00294 00295 Akonadi::Collection::Rights CollectionDialog::accessRightsFilter() const 00296 { 00297 return d->mRightsFilterModel->accessRights(); 00298 } 00299 00300 void CollectionDialog::setDescription( const QString &text ) 00301 { 00302 d->mTextLabel->setText( text ); 00303 d->mTextLabel->show(); 00304 } 00305 00306 void CollectionDialog::setDefaultCollection( const Collection &collection ) 00307 { 00308 d->mSelectionHandler->waitForCollection( collection ); 00309 } 00310 00311 void CollectionDialog::setSelectionMode( QAbstractItemView::SelectionMode mode ) 00312 { 00313 d->mView->setSelectionMode( mode ); 00314 } 00315 00316 QAbstractItemView::SelectionMode CollectionDialog::selectionMode() const 00317 { 00318 return d->mView->selectionMode(); 00319 } 00320 00321 void CollectionDialog::changeCollectionDialogOptions( CollectionDialogOptions options ) 00322 { 00323 d->changeCollectionDialogOptions( options ); 00324 } 00325 00326 #include "collectiondialog.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Apr 30 2012 21:49:14 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Apr 30 2012 21:49:14 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.