20 #include "collectiondialog_mobile_p.h"
21 #include "asyncselectionhandler_p.h"
22 #include "collectiondialog.h"
24 #include <qplatformdefs.h>
26 #include <kdescendantsproxymodel.h>
28 #include <akonadi/changerecorder.h>
29 #include <akonadi/collectioncreatejob.h>
30 #include <akonadi/collectionfilterproxymodel.h>
31 #include <akonadi/collectionutils_p.h>
32 #include <akonadi/entityrightsfiltermodel.h>
33 #include <akonadi/entitytreemodel.h>
36 #include <KInputDialog>
37 #include <KMessageBox>
38 #include <KStandardDirs>
40 #include <QtGui/QApplication>
41 #include <QtDeclarative/QDeclarativeContext>
42 #include <QtDeclarative/QDeclarativeEngine>
43 #include <QtDeclarative/QDeclarativeView>
45 using namespace Akonadi;
47 CollectionDialog::Private::Private( QAbstractItemModel *customModel, CollectionDialog *parent, CollectionDialogOptions options )
50 mSelectionMode( QAbstractItemView::SingleSelection ),
51 mOkButtonEnabled( false ),
52 mCancelButtonEnabled( true ),
53 mCreateButtonEnabled( false )
56 mView =
new QDeclarativeView( mParent );
57 mView->setResizeMode( QDeclarativeView::SizeRootObjectToView );
59 mParent->setMainWidget( mView );
60 mParent->setButtons( KDialog::None );
64 QAbstractItemModel *baseModel;
67 baseModel = customModel;
70 mMonitor->fetchCollection(
true );
71 mMonitor->setCollectionMonitored( Akonadi::Collection::root() );
73 mModel =
new EntityTreeModel( mMonitor, mParent );
74 mModel->setItemPopulationStrategy( EntityTreeModel::NoItemPopulation );
79 KDescendantsProxyModel *proxyModel =
new KDescendantsProxyModel( parent );
80 proxyModel->setDisplayAncestorData(
true );
81 proxyModel->setSourceModel( baseModel );
83 mMimeTypeFilterModel =
new CollectionFilterProxyModel( parent );
84 mMimeTypeFilterModel->setSourceModel( proxyModel );
86 mRightsFilterModel =
new EntityRightsFilterModel( parent );
87 mRightsFilterModel->setSourceModel( mMimeTypeFilterModel );
89 mFilterModel =
new QSortFilterProxyModel( parent );
90 mFilterModel->setFilterCaseSensitivity( Qt::CaseInsensitive );
91 mFilterModel->setSourceModel( mRightsFilterModel );
93 mSelectionModel =
new QItemSelectionModel( mFilterModel );
94 mParent->connect( mSelectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
95 SLOT(slotSelectionChanged()) );
96 mParent->connect( mSelectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
97 this, SLOT(selectionChanged(QItemSelection,QItemSelection)) );
100 mParent->connect( mSelectionHandler, SIGNAL(collectionAvailable(QModelIndex)),
101 SLOT(slotCollectionAvailable(QModelIndex)) );
103 foreach (
const QString &importPath, KGlobal::dirs()->findDirs(
"module", QLatin1String(
"imports" ) ) )
104 mView->engine()->addImportPath( importPath );
106 mView->rootContext()->setContextProperty( QLatin1String(
"dialogController" ),
this );
107 mView->rootContext()->setContextProperty( QLatin1String(
"collectionModel" ), mFilterModel );
110 mView->rootContext()->setContextProperty( QLatin1String(
"okButtonText" ), KStandardGuiItem::ok().text().
remove( QLatin1Char(
'&' ) ) );
111 mView->rootContext()->setContextProperty( QLatin1String(
"cancelButtonText" ), KStandardGuiItem::cancel().text().
remove( QLatin1Char(
'&' ) ) );
112 mView->rootContext()->setContextProperty( QLatin1String(
"createButtonText" ), i18n(
"&New Subfolder..." ).
remove( QLatin1Char(
'&' ) ) );
114 mView->setSource( KUrl::fromLocalFile( KStandardDirs::locate(
"data", QLatin1String(
"akonadi-kde/qml/CollectionDialogMobile.qml" ) ) ) );
116 #if defined (Q_WS_MAEMO_5) || defined (Q_OS_WINCE) || defined (MEEGO_EDITION_HARMATTAN)
117 mParent->setWindowState( Qt::WindowFullScreen );
120 mParent->resize( 800, 480 );
124 CollectionDialog::Private::~Private()
128 void CollectionDialog::Private::slotCollectionAvailable(
const QModelIndex &index )
130 mSelectionModel->setCurrentIndex( index, QItemSelectionModel::ClearAndSelect );
133 void CollectionDialog::Private::slotSelectionChanged()
135 mOkButtonEnabled = mSelectionModel->hasSelection();
136 if ( mAllowToCreateNewChildCollection ) {
137 const Akonadi::Collection parentCollection = mParent->selectedCollection();
138 const bool canCreateChildCollections = canCreateCollection( parentCollection );
139 const bool isVirtual = Akonadi::CollectionUtils::isVirtual( parentCollection );
141 mCreateButtonEnabled = (canCreateChildCollections && !isVirtual);
142 if ( parentCollection.isValid() ) {
144 mOkButtonEnabled = canCreateItems;
148 emit buttonStatusChanged();
151 void CollectionDialog::Private::changeCollectionDialogOptions( CollectionDialogOptions options )
153 mAllowToCreateNewChildCollection = ( options & AllowToCreateNewChildCollection );
154 emit buttonStatusChanged();
157 bool CollectionDialog::Private::canCreateCollection(
const Akonadi::Collection &parentCollection )
const
159 if ( !parentCollection.isValid() )
163 const QStringList dialogMimeTypeFilter = mParent->mimeTypeFilter();
164 const QStringList parentCollectionMimeTypes = parentCollection.contentMimeTypes();
165 Q_FOREACH (
const QString& mimetype, dialogMimeTypeFilter ) {
166 if ( parentCollectionMimeTypes.contains( mimetype ) )
174 void CollectionDialog::Private::slotAddChildCollection()
176 const Akonadi::Collection parentCollection = mParent->selectedCollection();
177 if ( canCreateCollection( parentCollection ) ) {
178 const QString name = KInputDialog::getText( i18nc(
"@title:window",
"New Folder" ),
179 i18nc(
"@label:textbox, name of a thing",
"Name" ),
180 QString(), 0, mParent );
181 if ( name.isEmpty() )
184 Akonadi::Collection collection;
185 collection.setName( name );
186 collection.setParentCollection( parentCollection );
187 Akonadi::CollectionCreateJob *job =
new Akonadi::CollectionCreateJob( collection );
188 connect( job, SIGNAL(result(KJob*)), mParent, SLOT(slotCollectionCreationResult(KJob*)) );
192 void CollectionDialog::Private::slotCollectionCreationResult( KJob* job )
194 if ( job->error() ) {
195 KMessageBox::error( mParent, i18n(
"Could not create folder: %1", job->errorString() ),
196 i18n(
"Folder creation failed" ) );
200 void CollectionDialog::Private::setDescriptionText(
const QString &text )
202 mDescriptionText = text;
203 emit descriptionTextChanged();
206 QString CollectionDialog::Private::descriptionText()
const
208 return mDescriptionText;
211 bool CollectionDialog::Private::okButtonEnabled()
const
213 return mOkButtonEnabled;
216 bool CollectionDialog::Private::cancelButtonEnabled()
const
218 return mCancelButtonEnabled;
221 bool CollectionDialog::Private::createButtonEnabled()
const
223 return mCreateButtonEnabled;
226 bool CollectionDialog::Private::createButtonVisible()
const
228 return mAllowToCreateNewChildCollection;
231 void CollectionDialog::Private::okClicked()
236 void CollectionDialog::Private::cancelClicked()
241 void CollectionDialog::Private::createClicked()
243 slotAddChildCollection();
246 void CollectionDialog::Private::setCurrentIndex(
int row )
248 const QModelIndex index = mSelectionModel->model()->index( row, 0 );
249 mSelectionModel->select( index, QItemSelectionModel::ClearAndSelect );
252 void CollectionDialog::Private::setFilterText(
const QString &text )
254 mFilterModel->setFilterFixedString( text );
257 void CollectionDialog::Private::selectionChanged(
const QItemSelection &selection,
const QItemSelection& )
259 if ( selection.isEmpty() )
262 emit selectionChanged( selection.indexes().first().row() );
265 CollectionDialog::CollectionDialog( QWidget *parent )
266 : KDialog( parent, Qt::Window ),
267 d( new Private( 0, this, CollectionDialog::None ) )
271 CollectionDialog::CollectionDialog( QAbstractItemModel *model, QWidget *parent )
272 : KDialog( parent, Qt::Window ),
273 d( new Private( model, this, CollectionDialog::None ) )
277 CollectionDialog::CollectionDialog( CollectionDialogOptions options, QAbstractItemModel *model, QWidget *parent )
278 : KDialog( parent, Qt::Window ),
279 d( new Private( model, this, options ) )
284 CollectionDialog::~CollectionDialog()
288 Akonadi::Collection CollectionDialog::selectedCollection()
const
290 if ( !d->mSelectionModel->hasSelection() )
291 return Akonadi::Collection();
293 return d->mSelectionModel->selectedRows().first().data( Akonadi::EntityTreeModel::CollectionRole ).value<Akonadi::Collection>();
298 if ( !d->mSelectionModel->hasSelection() )
299 return Akonadi::Collection::List();
301 return (Akonadi::Collection::List() << d->mSelectionModel->selectedRows().first().data( Akonadi::EntityTreeModel::CollectionRole ).value<Akonadi::Collection>());
304 void CollectionDialog::setMimeTypeFilter(
const QStringList &mimeTypes )
306 d->mMimeTypeFilterModel->clearFilters();
307 d->mMimeTypeFilterModel->addMimeTypeFilters( mimeTypes );
310 QStringList CollectionDialog::mimeTypeFilter()
const
312 return d->mMimeTypeFilterModel->mimeTypes();
315 void CollectionDialog::setAccessRightsFilter( Collection::Rights rights )
317 d->mRightsFilterModel->setAccessRights( rights );
320 Akonadi::Collection::Rights CollectionDialog::accessRightsFilter()
const
322 return d->mRightsFilterModel->accessRights();
325 void CollectionDialog::setDescription(
const QString &text )
327 d->setDescriptionText( text );
330 void CollectionDialog::setDefaultCollection(
const Collection &collection )
332 d->mSelectionHandler->waitForCollection( collection );
335 void CollectionDialog::setSelectionMode( QAbstractItemView::SelectionMode mode )
337 d->mSelectionMode = mode;
340 QAbstractItemView::SelectionMode CollectionDialog::selectionMode()
const
342 return d->mSelectionMode;
345 void CollectionDialog::changeCollectionDialogOptions( CollectionDialogOptions options )
347 d->changeCollectionDialogOptions( options );
350 #include "collectiondialog.moc"
351 #include "collectiondialog_mobile_p.moc"