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>
38 #include <KMessageBox>
39 #include <KStandardDirs>
41 #include <QApplication>
42 #include <QDeclarativeContext>
43 #include <QDeclarativeEngine>
44 #include <QDeclarativeView>
46 using namespace Akonadi;
48 CollectionDialog::Private::Private( QAbstractItemModel *customModel,
CollectionDialog *parent, CollectionDialogOptions options )
51 mSelectionMode( QAbstractItemView::SingleSelection ),
52 mOkButtonEnabled( false ),
53 mCancelButtonEnabled( true ),
54 mCreateButtonEnabled( false )
57 mView =
new QDeclarativeView( mParent );
58 mView->setResizeMode( QDeclarativeView::SizeRootObjectToView );
60 mParent->setMainWidget( mView );
61 mParent->setButtons( KDialog::None );
65 QAbstractItemModel *baseModel;
68 baseModel = customModel;
71 mMonitor->fetchCollection(
true );
80 KDescendantsProxyModel *proxyModel =
new KDescendantsProxyModel( parent );
81 proxyModel->setDisplayAncestorData(
true );
82 proxyModel->setSourceModel( baseModel );
85 mMimeTypeFilterModel->setSourceModel( proxyModel );
88 mRightsFilterModel->setSourceModel( mMimeTypeFilterModel );
90 mFilterModel =
new QSortFilterProxyModel( parent );
91 mFilterModel->setFilterCaseSensitivity( Qt::CaseInsensitive );
92 mFilterModel->setSourceModel( mRightsFilterModel );
94 mSelectionModel =
new QItemSelectionModel( mFilterModel );
95 mParent->connect( mSelectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
96 SLOT(slotSelectionChanged()) );
97 mParent->connect( mSelectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
98 this, SLOT(selectionChanged(QItemSelection,QItemSelection)) );
101 mParent->connect( mSelectionHandler, SIGNAL(collectionAvailable(QModelIndex)),
102 SLOT(slotCollectionAvailable(QModelIndex)) );
104 foreach (
const QString &importPath, KGlobal::dirs()->findDirs(
"module", QLatin1String(
"imports" ) ) ) {
105 mView->engine()->addImportPath( importPath );
108 mView->rootContext()->setContextProperty( QLatin1String(
"dialogController" ),
this );
109 mView->rootContext()->setContextProperty( QLatin1String(
"collectionModel" ), mFilterModel );
112 mView->rootContext()->setContextProperty( QLatin1String(
"okButtonText" ), KStandardGuiItem::ok().text().
remove( QLatin1Char(
'&' ) ) );
113 mView->rootContext()->setContextProperty( QLatin1String(
"cancelButtonText" ), KStandardGuiItem::cancel().text().
remove( QLatin1Char(
'&' ) ) );
114 mView->rootContext()->setContextProperty( QLatin1String(
"createButtonText" ), i18n(
"&New Subfolder..." ).
remove( QLatin1Char(
'&' ) ) );
116 mView->setSource( KUrl::fromLocalFile( KStandardDirs::locate(
"data", QLatin1String(
"akonadi-kde/qml/CollectionDialogMobile.qml" ) ) ) );
118 #if defined (Q_WS_MAEMO_5) || defined (Q_OS_WINCE) || defined (MEEGO_EDITION_HARMATTAN)
119 mParent->setWindowState( Qt::WindowFullScreen );
122 mParent->resize( 800, 480 );
126 CollectionDialog::Private::~Private()
130 void CollectionDialog::Private::slotCollectionAvailable(
const QModelIndex &index )
132 mSelectionModel->setCurrentIndex( index, QItemSelectionModel::ClearAndSelect );
135 void CollectionDialog::Private::slotSelectionChanged()
137 mOkButtonEnabled = mSelectionModel->hasSelection();
138 if ( mAllowToCreateNewChildCollection ) {
140 const bool canCreateChildCollections = canCreateCollection( parentCollection );
141 const bool isVirtual = parentCollection.
isVirtual();
143 mCreateButtonEnabled = ( canCreateChildCollections && !isVirtual );
144 if ( parentCollection.
isValid() ) {
146 mOkButtonEnabled = canCreateItems;
150 emit buttonStatusChanged();
153 void CollectionDialog::Private::changeCollectionDialogOptions( CollectionDialogOptions options )
155 mAllowToCreateNewChildCollection = ( options & AllowToCreateNewChildCollection );
156 emit buttonStatusChanged();
159 bool CollectionDialog::Private::canCreateCollection(
const Akonadi::Collection &parentCollection )
const
161 if ( !parentCollection.
isValid() ) {
166 const QStringList dialogMimeTypeFilter = mParent->mimeTypeFilter();
167 const QStringList parentCollectionMimeTypes = parentCollection.
contentMimeTypes();
168 Q_FOREACH (
const QString& mimetype, dialogMimeTypeFilter ) {
169 if ( parentCollectionMimeTypes.contains( mimetype ) ) {
178 void CollectionDialog::Private::slotAddChildCollection()
181 if ( canCreateCollection( parentCollection ) ) {
182 const QString name = KInputDialog::getText( i18nc(
"@title:window",
"New Folder" ),
183 i18nc(
"@label:textbox, name of a thing",
"Name" ),
184 QString(), 0, mParent );
185 if ( name.isEmpty() ) {
193 connect( job, SIGNAL(result(KJob*)), mParent, SLOT(slotCollectionCreationResult(KJob*)) );
197 void CollectionDialog::Private::slotCollectionCreationResult( KJob* job )
199 if ( job->error() ) {
200 KMessageBox::error( mParent, i18n(
"Could not create folder: %1", job->errorString() ),
201 i18n(
"Folder creation failed" ) );
205 void CollectionDialog::Private::setDescriptionText(
const QString &text )
207 mDescriptionText = text;
208 emit descriptionTextChanged();
211 QString CollectionDialog::Private::descriptionText()
const
213 return mDescriptionText;
216 bool CollectionDialog::Private::okButtonEnabled()
const
218 return mOkButtonEnabled;
221 bool CollectionDialog::Private::cancelButtonEnabled()
const
223 return mCancelButtonEnabled;
226 bool CollectionDialog::Private::createButtonEnabled()
const
228 return mCreateButtonEnabled;
231 bool CollectionDialog::Private::createButtonVisible()
const
233 return mAllowToCreateNewChildCollection;
236 void CollectionDialog::Private::okClicked()
241 void CollectionDialog::Private::cancelClicked()
246 void CollectionDialog::Private::createClicked()
248 slotAddChildCollection();
251 void CollectionDialog::Private::setCurrentIndex(
int row )
253 const QModelIndex index = mSelectionModel->model()->index( row, 0 );
254 mSelectionModel->select( index, QItemSelectionModel::ClearAndSelect );
257 void CollectionDialog::Private::setFilterText(
const QString &text )
259 mFilterModel->setFilterFixedString( text );
262 void CollectionDialog::Private::selectionChanged(
const QItemSelection &selection,
const QItemSelection& )
264 if ( selection.isEmpty() ) {
268 emit selectionChanged( selection.indexes().first().row() );
272 : KDialog( parent, Qt::Window ),
278 : KDialog( parent, Qt::Window ),
284 : KDialog( parent, Qt::Window ),
285 d( new Private( model, this, options ) )
296 if ( !d->mSelectionModel->hasSelection() ) {
305 if ( !d->mSelectionModel->hasSelection() ) {
314 d->mMimeTypeFilterModel->clearFilters();
315 d->mMimeTypeFilterModel->addMimeTypeFilters( mimeTypes );
320 return d->mMimeTypeFilterModel->mimeTypes();
325 d->mRightsFilterModel->setAccessRights( rights );
330 return d->mRightsFilterModel->accessRights();
335 d->setDescriptionText( text );
340 d->mSelectionHandler->waitForCollection( collection );
345 d->mSelectionMode = mode;
350 return d->mSelectionMode;
355 d->changeCollectionDialogOptions( options );
358 #include "collectiondialog.moc"
359 #include "moc_collectiondialog_mobile_p.cpp"