akonadi
collectiondialog_mobile.cpp
00001 /* 00002 Copyright 2010 Tobias Koenig <tokoe@kde.org> 00003 00004 This library is free software; you can redistribute it and/or modify it 00005 under the terms of the GNU Library General Public License as published by 00006 the Free Software Foundation; either version 2 of the License, or (at your 00007 option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, but WITHOUT 00010 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00011 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00012 License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to the 00016 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00017 02110-1301, USA. 00018 */ 00019 00020 #include "collectiondialog_mobile_p.h" 00021 #include "asyncselectionhandler_p.h" 00022 #include "collectiondialog.h" 00023 00024 #include <qplatformdefs.h> 00025 00026 #include <kdescendantsproxymodel.h> 00027 00028 #include <akonadi/changerecorder.h> 00029 #include <akonadi/collectioncreatejob.h> 00030 #include <akonadi/collectionfilterproxymodel.h> 00031 #include <akonadi/collectionutils_p.h> 00032 #include <akonadi/entityrightsfiltermodel.h> 00033 #include <akonadi/entitytreemodel.h> 00034 00035 #include <KLocale> 00036 #include <KInputDialog> 00037 #include <KMessageBox> 00038 #include <KStandardDirs> 00039 00040 #include <QtGui/QApplication> 00041 #include <QtDeclarative/QDeclarativeContext> 00042 #include <QtDeclarative/QDeclarativeEngine> 00043 #include <QtDeclarative/QDeclarativeView> 00044 00045 using namespace Akonadi; 00046 00047 CollectionDialog::Private::Private( QAbstractItemModel *customModel, CollectionDialog *parent, CollectionDialogOptions options ) 00048 : QObject( parent ), 00049 mParent( parent ), 00050 mSelectionMode( QAbstractItemView::SingleSelection ), 00051 mOkButtonEnabled( false ), 00052 mCancelButtonEnabled( true ), 00053 mCreateButtonEnabled( false ) 00054 { 00055 // setup GUI 00056 mView = new QDeclarativeView( mParent ); 00057 mView->setResizeMode( QDeclarativeView::SizeRootObjectToView ); 00058 00059 mParent->setMainWidget( mView ); 00060 mParent->setButtons( KDialog::None ); 00061 00062 changeCollectionDialogOptions( options ); 00063 00064 QAbstractItemModel *baseModel; 00065 00066 if ( customModel ) { 00067 baseModel = customModel; 00068 } else { 00069 mMonitor = new Akonadi::ChangeRecorder( mParent ); 00070 mMonitor->fetchCollection( true ); 00071 mMonitor->setCollectionMonitored( Akonadi::Collection::root() ); 00072 00073 mModel = new EntityTreeModel( mMonitor, mParent ); 00074 mModel->setItemPopulationStrategy( EntityTreeModel::NoItemPopulation ); 00075 00076 baseModel = mModel; 00077 } 00078 00079 KDescendantsProxyModel *proxyModel = new KDescendantsProxyModel( parent ); 00080 proxyModel->setDisplayAncestorData( true ); 00081 proxyModel->setSourceModel( baseModel ); 00082 00083 mMimeTypeFilterModel = new CollectionFilterProxyModel( parent ); 00084 mMimeTypeFilterModel->setSourceModel( proxyModel ); 00085 00086 mRightsFilterModel = new EntityRightsFilterModel( parent ); 00087 mRightsFilterModel->setSourceModel( mMimeTypeFilterModel ); 00088 00089 mFilterModel = new QSortFilterProxyModel( parent ); 00090 mFilterModel->setFilterCaseSensitivity( Qt::CaseInsensitive ); 00091 mFilterModel->setSourceModel( mRightsFilterModel ); 00092 00093 mSelectionModel = new QItemSelectionModel( mFilterModel ); 00094 mParent->connect( mSelectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)), 00095 SLOT(slotSelectionChanged()) ); 00096 mParent->connect( mSelectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)), 00097 this, SLOT(selectionChanged(QItemSelection,QItemSelection)) ); 00098 00099 mSelectionHandler = new AsyncSelectionHandler( mFilterModel, mParent ); 00100 mParent->connect( mSelectionHandler, SIGNAL(collectionAvailable(QModelIndex)), 00101 SLOT(slotCollectionAvailable(QModelIndex)) ); 00102 00103 foreach ( const QString &importPath, KGlobal::dirs()->findDirs( "module", QLatin1String( "imports" ) ) ) 00104 mView->engine()->addImportPath( importPath ); 00105 00106 mView->rootContext()->setContextProperty( QLatin1String( "dialogController" ), this ); 00107 mView->rootContext()->setContextProperty( QLatin1String( "collectionModel" ), mFilterModel ); 00108 00109 // QUICKHACK: since we have no KDE integration plugin available in kdelibs, we have to do the translation in C++ space 00110 mView->rootContext()->setContextProperty( QLatin1String( "okButtonText" ), KStandardGuiItem::ok().text().remove( QLatin1Char( '&' ) ) ); 00111 mView->rootContext()->setContextProperty( QLatin1String( "cancelButtonText" ), KStandardGuiItem::cancel().text().remove( QLatin1Char( '&' ) ) ); 00112 mView->rootContext()->setContextProperty( QLatin1String( "createButtonText" ), i18n( "&New Subfolder..." ).remove( QLatin1Char( '&' ) ) ); 00113 00114 mView->setSource( KUrl::fromLocalFile( KStandardDirs::locate( "data", QLatin1String( "akonadi-kde/qml/CollectionDialogMobile.qml" ) ) ) ); 00115 00116 #if defined (Q_WS_MAEMO_5) || defined (Q_OS_WINCE) || defined (MEEGO_EDITION_HARMATTAN) 00117 mParent->setWindowState( Qt::WindowFullScreen ); 00118 #else 00119 // on the desktop start with a nice size 00120 mParent->resize( 800, 480 ); 00121 #endif 00122 } 00123 00124 CollectionDialog::Private::~Private() 00125 { 00126 } 00127 00128 void CollectionDialog::Private::slotCollectionAvailable( const QModelIndex &index ) 00129 { 00130 mSelectionModel->setCurrentIndex( index, QItemSelectionModel::ClearAndSelect ); 00131 } 00132 00133 void CollectionDialog::Private::slotSelectionChanged() 00134 { 00135 mOkButtonEnabled = mSelectionModel->hasSelection(); 00136 if ( mAllowToCreateNewChildCollection ) { 00137 const Akonadi::Collection parentCollection = mParent->selectedCollection(); 00138 const bool canCreateChildCollections = canCreateCollection( parentCollection ); 00139 const bool isVirtual = Akonadi::CollectionUtils::isVirtual( parentCollection ); 00140 00141 mCreateButtonEnabled = (canCreateChildCollections && !isVirtual); 00142 if ( parentCollection.isValid() ) { 00143 const bool canCreateItems = (parentCollection.rights() & Akonadi::Collection::CanCreateItem); 00144 mOkButtonEnabled = canCreateItems; 00145 } 00146 } 00147 00148 emit buttonStatusChanged(); 00149 } 00150 00151 void CollectionDialog::Private::changeCollectionDialogOptions( CollectionDialogOptions options ) 00152 { 00153 mAllowToCreateNewChildCollection = ( options & AllowToCreateNewChildCollection ); 00154 emit buttonStatusChanged(); 00155 } 00156 00157 bool CollectionDialog::Private::canCreateCollection( const Akonadi::Collection &parentCollection ) const 00158 { 00159 if ( !parentCollection.isValid() ) 00160 return false; 00161 00162 if ( ( parentCollection.rights() & Akonadi::Collection::CanCreateCollection ) ) { 00163 const QStringList dialogMimeTypeFilter = mParent->mimeTypeFilter(); 00164 const QStringList parentCollectionMimeTypes = parentCollection.contentMimeTypes(); 00165 Q_FOREACH ( const QString& mimetype, dialogMimeTypeFilter ) { 00166 if ( parentCollectionMimeTypes.contains( mimetype ) ) 00167 return true; 00168 } 00169 return true; 00170 } 00171 return false; 00172 } 00173 00174 void CollectionDialog::Private::slotAddChildCollection() 00175 { 00176 const Akonadi::Collection parentCollection = mParent->selectedCollection(); 00177 if ( canCreateCollection( parentCollection ) ) { 00178 const QString name = KInputDialog::getText( i18nc( "@title:window", "New Folder" ), 00179 i18nc( "@label:textbox, name of a thing", "Name" ), 00180 QString(), 0, mParent ); 00181 if ( name.isEmpty() ) 00182 return; 00183 00184 Akonadi::Collection collection; 00185 collection.setName( name ); 00186 collection.setParentCollection( parentCollection ); 00187 Akonadi::CollectionCreateJob *job = new Akonadi::CollectionCreateJob( collection ); 00188 connect( job, SIGNAL(result(KJob*)), mParent, SLOT(slotCollectionCreationResult(KJob*)) ); 00189 } 00190 } 00191 00192 void CollectionDialog::Private::slotCollectionCreationResult( KJob* job ) 00193 { 00194 if ( job->error() ) { 00195 KMessageBox::error( mParent, i18n( "Could not create folder: %1", job->errorString() ), 00196 i18n( "Folder creation failed" ) ); 00197 } 00198 } 00199 00200 void CollectionDialog::Private::setDescriptionText( const QString &text ) 00201 { 00202 mDescriptionText = text; 00203 emit descriptionTextChanged(); 00204 } 00205 00206 QString CollectionDialog::Private::descriptionText() const 00207 { 00208 return mDescriptionText; 00209 } 00210 00211 bool CollectionDialog::Private::okButtonEnabled() const 00212 { 00213 return mOkButtonEnabled; 00214 } 00215 00216 bool CollectionDialog::Private::cancelButtonEnabled() const 00217 { 00218 return mCancelButtonEnabled; 00219 } 00220 00221 bool CollectionDialog::Private::createButtonEnabled() const 00222 { 00223 return mCreateButtonEnabled; 00224 } 00225 00226 bool CollectionDialog::Private::createButtonVisible() const 00227 { 00228 return mAllowToCreateNewChildCollection; 00229 } 00230 00231 void CollectionDialog::Private::okClicked() 00232 { 00233 mParent->accept(); 00234 } 00235 00236 void CollectionDialog::Private::cancelClicked() 00237 { 00238 mParent->reject(); 00239 } 00240 00241 void CollectionDialog::Private::createClicked() 00242 { 00243 slotAddChildCollection(); 00244 } 00245 00246 void CollectionDialog::Private::setCurrentIndex( int row ) 00247 { 00248 const QModelIndex index = mSelectionModel->model()->index( row, 0 ); 00249 mSelectionModel->select( index, QItemSelectionModel::ClearAndSelect ); 00250 } 00251 00252 void CollectionDialog::Private::setFilterText( const QString &text ) 00253 { 00254 mFilterModel->setFilterFixedString( text ); 00255 } 00256 00257 void CollectionDialog::Private::selectionChanged( const QItemSelection &selection, const QItemSelection& ) 00258 { 00259 if ( selection.isEmpty() ) 00260 return; 00261 00262 emit selectionChanged( selection.indexes().first().row() ); 00263 } 00264 00265 CollectionDialog::CollectionDialog( QWidget *parent ) 00266 : KDialog( parent, Qt::Window ), 00267 d( new Private( 0, this, CollectionDialog::None ) ) 00268 { 00269 } 00270 00271 CollectionDialog::CollectionDialog( QAbstractItemModel *model, QWidget *parent ) 00272 : KDialog( parent, Qt::Window ), 00273 d( new Private( model, this, CollectionDialog::None ) ) 00274 { 00275 } 00276 00277 CollectionDialog::CollectionDialog( CollectionDialogOptions options, QAbstractItemModel *model, QWidget *parent ) 00278 : KDialog( parent, Qt::Window ), 00279 d( new Private( model, this, options ) ) 00280 { 00281 } 00282 00283 00284 CollectionDialog::~CollectionDialog() 00285 { 00286 } 00287 00288 Akonadi::Collection CollectionDialog::selectedCollection() const 00289 { 00290 if ( !d->mSelectionModel->hasSelection() ) 00291 return Akonadi::Collection(); 00292 00293 return d->mSelectionModel->selectedRows().first().data( Akonadi::EntityTreeModel::CollectionRole ).value<Akonadi::Collection>(); 00294 } 00295 00296 Akonadi::Collection::List CollectionDialog::selectedCollections() const 00297 { 00298 if ( !d->mSelectionModel->hasSelection() ) 00299 return Akonadi::Collection::List(); 00300 00301 return (Akonadi::Collection::List() << d->mSelectionModel->selectedRows().first().data( Akonadi::EntityTreeModel::CollectionRole ).value<Akonadi::Collection>()); 00302 } 00303 00304 void CollectionDialog::setMimeTypeFilter( const QStringList &mimeTypes ) 00305 { 00306 d->mMimeTypeFilterModel->clearFilters(); 00307 d->mMimeTypeFilterModel->addMimeTypeFilters( mimeTypes ); 00308 } 00309 00310 QStringList CollectionDialog::mimeTypeFilter() const 00311 { 00312 return d->mMimeTypeFilterModel->mimeTypes(); 00313 } 00314 00315 void CollectionDialog::setAccessRightsFilter( Collection::Rights rights ) 00316 { 00317 d->mRightsFilterModel->setAccessRights( rights ); 00318 } 00319 00320 Akonadi::Collection::Rights CollectionDialog::accessRightsFilter() const 00321 { 00322 return d->mRightsFilterModel->accessRights(); 00323 } 00324 00325 void CollectionDialog::setDescription( const QString &text ) 00326 { 00327 d->setDescriptionText( text ); 00328 } 00329 00330 void CollectionDialog::setDefaultCollection( const Collection &collection ) 00331 { 00332 d->mSelectionHandler->waitForCollection( collection ); 00333 } 00334 00335 void CollectionDialog::setSelectionMode( QAbstractItemView::SelectionMode mode ) 00336 { 00337 d->mSelectionMode = mode; 00338 } 00339 00340 QAbstractItemView::SelectionMode CollectionDialog::selectionMode() const 00341 { 00342 return d->mSelectionMode; 00343 } 00344 00345 void CollectionDialog::changeCollectionDialogOptions( CollectionDialogOptions options ) 00346 { 00347 d->changeCollectionDialogOptions( options ); 00348 } 00349 00350 #include "collectiondialog.moc" 00351 #include "collectiondialog_mobile_p.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu Aug 2 2012 15:25:17 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu Aug 2 2012 15:25:17 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.