21 #include "collectiondialog.h"
23 #include "asyncselectionhandler_p.h"
25 #include <akonadi/changerecorder.h>
26 #include <akonadi/collectionfetchscope.h>
27 #include <akonadi/collectionfilterproxymodel.h>
28 #include <akonadi/entityrightsfiltermodel.h>
29 #include <akonadi/entitytreemodel.h>
30 #include <akonadi/entitytreeview.h>
31 #include <akonadi/session.h>
32 #include <akonadi/collectioncreatejob.h>
33 #include <akonadi/collectionutils_p.h>
35 #include <QHeaderView>
37 #include <QVBoxLayout>
41 #include <KInputDialog>
42 #include <KMessageBox>
44 using namespace Akonadi;
46 class CollectionDialog::Private
49 Private( QAbstractItemModel *customModel,
CollectionDialog *parent, CollectionDialogOptions options )
54 QWidget *widget = mParent->mainWidget();
55 QVBoxLayout *layout =
new QVBoxLayout( widget );
56 layout->setContentsMargins( 0, 0, 0, 0 );
60 mTextLabel =
new QLabel;
61 layout->addWidget( mTextLabel );
64 KLineEdit* filterCollectionLineEdit =
new KLineEdit( widget );
65 filterCollectionLineEdit->setClearButtonShown(
true );
66 filterCollectionLineEdit->setClickMessage( i18nc(
"@info/plain Displayed grayed-out inside the "
67 "textbox, verb to search",
"Search" ) );
68 layout->addWidget( filterCollectionLineEdit );
71 mView->setDragDropMode( QAbstractItemView::NoDragDrop );
72 mView->header()->hide();
73 layout->addWidget( mView );
76 mParent->enableButton( KDialog::Ok,
false );
79 QAbstractItemModel *baseModel;
82 baseModel = customModel;
85 mMonitor->fetchCollection(
true );
94 mMimeTypeFilterModel->setSourceModel( baseModel );
95 mMimeTypeFilterModel->setExcludeVirtualCollections(
true );
98 mRightsFilterModel->setSourceModel( mMimeTypeFilterModel );
100 KRecursiveFilterProxyModel* filterCollection =
new KRecursiveFilterProxyModel( mParent );
101 filterCollection->setDynamicSortFilter(
true );
102 filterCollection->setSourceModel( mRightsFilterModel );
103 filterCollection->setFilterCaseSensitivity( Qt::CaseInsensitive );
104 mView->setModel( filterCollection );
106 mParent->connect( filterCollectionLineEdit, SIGNAL(textChanged(QString)),
107 filterCollection, SLOT(setFilterFixedString(QString)) );
109 mParent->connect( mView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
110 mParent, SLOT(slotSelectionChanged()) );
112 mParent->connect( mView, SIGNAL(doubleClicked(QModelIndex)),
113 mParent, SLOT(accept()) );
116 mParent->connect( mSelectionHandler, SIGNAL(collectionAvailable(QModelIndex)),
117 mParent, SLOT(slotCollectionAvailable(QModelIndex)) );
124 void slotCollectionAvailable(
const QModelIndex &index )
127 mView->setCurrentIndex( index );
138 bool mAllowToCreateNewChildCollection;
139 bool mKeepTreeExpanded;
141 void slotSelectionChanged();
142 void slotAddChildCollection();
143 void slotCollectionCreationResult( KJob* job );
149 void CollectionDialog::Private::slotSelectionChanged()
151 mParent->enableButton( KDialog::Ok, mView->selectionModel()->selectedIndexes().count() > 0 );
152 if ( mAllowToCreateNewChildCollection ) {
154 const bool canCreateChildCollections = canCreateCollection( parentCollection );
156 mParent->enableButton( KDialog::User1, ( canCreateChildCollections && !parentCollection.
isVirtual() ) );
157 if ( parentCollection.
isValid() ) {
159 mParent->enableButton( KDialog::Ok, canCreateItems );
164 void CollectionDialog::Private::changeCollectionDialogOptions( CollectionDialogOptions options )
166 mAllowToCreateNewChildCollection = ( options & AllowToCreateNewChildCollection );
167 if ( mAllowToCreateNewChildCollection ) {
168 mParent->setButtons( Ok | Cancel | User1 );
169 mParent->setButtonGuiItem( User1, KGuiItem( i18n(
"&New Subfolder..." ), QLatin1String(
"folder-new" ),
170 i18n(
"Create a new subfolder under the currently selected folder" ) ) );
171 mParent->enableButton( KDialog::User1,
false );
172 connect( mParent, SIGNAL(user1Clicked()), mParent, SLOT(slotAddChildCollection()) );
174 mKeepTreeExpanded = ( options & KeepTreeExpanded );
175 if ( mKeepTreeExpanded ) {
177 mParent->connect( mRightsFilterModel, SIGNAL(rowsInserted(QModelIndex,
int,
int)),
178 mView, SLOT(expandAll()), Qt::UniqueConnection );
185 bool CollectionDialog::Private::canCreateCollection(
const Akonadi::Collection &parentCollection )
const
187 if ( !parentCollection.
isValid() ) {
192 const QStringList dialogMimeTypeFilter = mParent->mimeTypeFilter();
193 const QStringList parentCollectionMimeTypes = parentCollection.
contentMimeTypes();
194 Q_FOREACH (
const QString& mimetype, dialogMimeTypeFilter ) {
195 if ( parentCollectionMimeTypes.contains( mimetype ) ) {
205 void CollectionDialog::Private::slotAddChildCollection()
208 if ( canCreateCollection( parentCollection ) ) {
209 const QString name = KInputDialog::getText( i18nc(
"@title:window",
"New Folder" ),
210 i18nc(
"@label:textbox, name of a thing",
"Name" ),
211 QString(), 0, mParent );
212 if ( name.isEmpty() ) {
220 connect( job, SIGNAL(result(KJob*)), mParent, SLOT(slotCollectionCreationResult(KJob*)) );
224 void CollectionDialog::Private::slotCollectionCreationResult( KJob* job )
226 if ( job->error() ) {
227 KMessageBox::error( mParent, i18n(
"Could not create folder: %1", job->errorString() ),
228 i18n(
"Folder creation failed" ) );
248 d( new Private( model, this, options ) )
260 if (
selectionMode() == QAbstractItemView::SingleSelection ) {
261 const QModelIndex index = d->mView->currentIndex();
262 if ( index.isValid() ) {
273 const QItemSelectionModel *selectionModel = d->mView->selectionModel();
274 const QModelIndexList selectedIndexes = selectionModel->selectedIndexes();
275 foreach (
const QModelIndex &index, selectedIndexes ) {
276 if ( index.isValid() ) {
279 collections.append( collection );
292 d->mMimeTypeFilterModel->clearFilters();
293 d->mMimeTypeFilterModel->addMimeTypeFilters( mimeTypes );
296 foreach (
const QString &mimetype, mimeTypes ) {
297 d->mMonitor->setMimeTypeMonitored( mimetype );
304 return d->mMimeTypeFilterModel->mimeTypeFilters();
311 d->mRightsFilterModel->setAccessRights( rights );
316 return d->mRightsFilterModel->accessRights();
321 d->mTextLabel->setText( text );
322 d->mTextLabel->show();
327 d->mSelectionHandler->waitForCollection( collection );
332 d->mView->setSelectionMode( mode );
337 return d->mView->selectionMode();
342 d->changeCollectionDialogOptions( options );
345 #include "collectiondialog.moc"