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>
41#include <KLocalizedString>
42#include <KInputDialog>
47class CollectionDialog::Private
50 Private(QAbstractItemModel *customModel,
CollectionDialog *parent, CollectionDialogOptions options)
55 QWidget *widget = mParent->mainWidget();
56 QVBoxLayout *layout =
new QVBoxLayout(widget);
57 layout->setContentsMargins(0, 0, 0, 0);
59 mTextLabel =
new QLabel;
60 layout->addWidget(mTextLabel);
63 KLineEdit *filterCollectionLineEdit =
new KLineEdit(widget);
64 filterCollectionLineEdit->setClearButtonShown(
true);
65 filterCollectionLineEdit->setClickMessage(i18nc(
"@info/plain Displayed grayed-out inside the "
66 "textbox, verb to search",
"Search"));
67 layout->addWidget(filterCollectionLineEdit);
70 mView->setDragDropMode(QAbstractItemView::NoDragDrop);
71 mView->header()->hide();
72 layout->addWidget(mView);
74 mUseByDefault =
new QCheckBox(i18n(
"Use folder by default"));
75 mUseByDefault->hide();
76 layout->addWidget(mUseByDefault);
78 mParent->enableButton(KDialog::Ok,
false);
81 QAbstractItemModel *baseModel;
84 baseModel = customModel;
96 mMimeTypeFilterModel->setSourceModel(baseModel);
100 mRightsFilterModel->setSourceModel(mMimeTypeFilterModel);
102 mFilterCollection =
new KRecursiveFilterProxyModel(mParent);
103 mFilterCollection->setDynamicSortFilter(
true);
104 mFilterCollection->setSourceModel(mRightsFilterModel);
105 mFilterCollection->setFilterCaseSensitivity(Qt::CaseInsensitive);
108 changeCollectionDialogOptions(options);
109 mParent->connect(filterCollectionLineEdit, SIGNAL(textChanged(QString)),
110 mParent, SLOT(slotFilterFixedString(QString)));
112 mParent->connect(mView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
113 mParent, SLOT(slotSelectionChanged()));
115 mParent->connect(mView, SIGNAL(doubleClicked(QModelIndex)),
116 mParent, SLOT(slotDoubleClicked()));
119 mParent->connect(mSelectionHandler, SIGNAL(collectionAvailable(QModelIndex)),
120 mParent, SLOT(slotCollectionAvailable(QModelIndex)));
129 void slotCollectionAvailable(
const QModelIndex &index)
132 mView->setCurrentIndex(index);
135 void slotFilterFixedString(
const QString &filter)
137 mFilterCollection->setFilterFixedString(filter);
138 if (mKeepTreeExpanded) {
145 KConfig config( QLatin1String(
"akonadi_contactrc" ) );
146 KConfigGroup group( &config, QLatin1String(
"CollectionDialog" ) );
147 const QSize size = group.readEntry(
"Size", QSize(800, 500) );
148 if ( size.isValid() ) {
149 mParent->resize( size );
155 KConfig config( QLatin1String(
"akonadi_contactrc" ) );
156 KConfigGroup group( &config, QLatin1String(
"CollectionDialog" ) );
157 group.writeEntry(
"Size", mParent->size() );
169 bool mAllowToCreateNewChildCollection;
170 bool mKeepTreeExpanded;
171 KRecursiveFilterProxyModel *mFilterCollection;
172 QCheckBox *mUseByDefault;
173 QStringList mContentMimeTypes;
175 void slotDoubleClicked();
176 void slotSelectionChanged();
177 void slotAddChildCollection();
178 void slotCollectionCreationResult(KJob *job);
180 void changeCollectionDialogOptions(CollectionDialogOptions options);
181 bool canSelectCollection()
const;
184void CollectionDialog::Private::slotDoubleClicked()
186 if (canSelectCollection()) {
191bool CollectionDialog::Private::canSelectCollection()
const
193 bool result = (mView->selectionModel()->selectedIndexes().count() > 0);
194 if (mAllowToCreateNewChildCollection) {
197 if (parentCollection.
isValid()) {
204void CollectionDialog::Private::slotSelectionChanged()
206 mParent->enableButton(KDialog::Ok, mView->selectionModel()->selectedIndexes().count() > 0);
207 if (mAllowToCreateNewChildCollection) {
209 const bool canCreateChildCollections = canCreateCollection(parentCollection);
211 mParent->enableButton(KDialog::User1, (canCreateChildCollections && !parentCollection.
isVirtual()));
212 if (parentCollection.
isValid()) {
214 mParent->enableButton(KDialog::Ok, canCreateItems);
219void CollectionDialog::Private::changeCollectionDialogOptions(CollectionDialogOptions options)
221 mAllowToCreateNewChildCollection = (options & AllowToCreateNewChildCollection);
222 if (mAllowToCreateNewChildCollection) {
223 mParent->setButtons(Ok | Cancel | User1);
224 mParent->setButtonGuiItem(User1, KGuiItem(i18n(
"&New Subfolder..."), QLatin1String(
"folder-new"),
225 i18n(
"Create a new subfolder under the currently selected folder")));
226 mParent->enableButton(KDialog::User1,
false);
227 connect(mParent, SIGNAL(user1Clicked()), mParent, SLOT(slotAddChildCollection()));
229 mKeepTreeExpanded = (options & KeepTreeExpanded);
230 if (mKeepTreeExpanded) {
231 mParent->connect(mRightsFilterModel, SIGNAL(rowsInserted(QModelIndex,
int,
int)),
232 mView, SLOT(expandAll()), Qt::UniqueConnection);
237bool CollectionDialog::Private::canCreateCollection(
const Akonadi::Collection &parentCollection)
const
239 if (!parentCollection.
isValid()) {
244 const QStringList dialogMimeTypeFilter = mParent->mimeTypeFilter();
245 const QStringList parentCollectionMimeTypes = parentCollection.
contentMimeTypes();
246 Q_FOREACH (
const QString &mimetype, dialogMimeTypeFilter) {
247 if (parentCollectionMimeTypes.contains(mimetype)) {
256void CollectionDialog::Private::slotAddChildCollection()
259 if (canCreateCollection(parentCollection)) {
260 const QString name = KInputDialog::getText(i18nc(
"@title:window",
"New Folder"),
261 i18nc(
"@label:textbox, name of a thing",
"Name"),
262 QString(), 0, mParent);
263 if (name.isEmpty()) {
268 collection.setName(name);
269 collection.setParentCollection(parentCollection);
270 if (!mContentMimeTypes.isEmpty()) {
271 collection.setContentMimeTypes(mContentMimeTypes);
274 connect(job, SIGNAL(result(KJob*)), mParent, SLOT(slotCollectionCreationResult(KJob*)));
278void CollectionDialog::Private::slotCollectionCreationResult(KJob *job)
281 KMessageBox::error(mParent, i18n(
"Could not create folder: %1", job->errorString()),
282 i18n(
"Folder creation failed"));
300 , d(new Private(model, this, options))
312 const QModelIndex index = d->mView->currentIndex();
313 if (index.isValid()) {
324 const QItemSelectionModel *selectionModel = d->mView->selectionModel();
325 const QModelIndexList selectedIndexes = selectionModel->selectedIndexes();
326 foreach (
const QModelIndex &index, selectedIndexes) {
327 if (index.isValid()) {
329 if (collection.isValid()) {
330 collections.append(collection);
344 d->mMimeTypeFilterModel->clearFilters();
345 d->mMimeTypeFilterModel->addMimeTypeFilters(mimeTypes);
348 foreach (
const QString &mimetype, mimeTypes) {
349 d->mMonitor->setMimeTypeMonitored(mimetype);
356 return d->mMimeTypeFilterModel->mimeTypeFilters();
364 d->mRightsFilterModel->setAccessRights(rights);
369 return d->mRightsFilterModel->accessRights();
374 d->mTextLabel->setText(text);
375 d->mTextLabel->show();
380 d->mSelectionHandler->waitForCollection(collection);
385 d->mView->setSelectionMode(mode);
390 return d->mView->selectionMode();
395 d->changeCollectionDialogOptions(options);
400 d->mUseByDefault->setChecked(b);
401 d->mUseByDefault->show();
406 return d->mUseByDefault->isChecked();
411 d->mContentMimeTypes = mimetypes;
415#include "moc_collectiondialog.cpp"
Records and replays change notification.
Job that creates a new collection in the Akonadi storage.
A collection selection dialog.
void setContentMimeTypes(const QStringList &mimetypes)
Allow to specify collection content mimetype when we create new one.
CollectionDialog(QWidget *parent=0)
Creates a new collection dialog.
void setUseFolderByDefault(bool b)
Akonadi::Collection selectedCollection() const
Returns the selected collection if the selection mode is QAbstractItemView::SingleSelection.
void changeCollectionDialogOptions(CollectionDialogOptions options)
Change collection dialog options.
QAbstractItemView::SelectionMode selectionMode() const
Returns the selection mode.
void setDefaultCollection(const Collection &collection)
Sets the collection that shall be selected by default.
void setSelectionMode(QAbstractItemView::SelectionMode mode)
Sets the selection mode.
void setAccessRightsFilter(Collection::Rights rights)
Sets the access rights that the listed collections shall match with.
~CollectionDialog()
Destroys the collection dialog.
QStringList mimeTypeFilter() const
Returns the mime types any of which the selected collection(s) shall support.
void setMimeTypeFilter(const QStringList &mimeTypes)
Sets the mime types any of which the selected collection(s) shall support.
void setDescription(const QString &text)
Sets the text that will be shown in the dialog.
Collection::Rights accessRightsFilter() const
Sets the access rights that the listed collections shall match with.
Akonadi::Collection::List selectedCollections() const
Returns the list of selected collections.
bool useFolderByDefault() const
A proxy model that filters collections by mime type.
void setExcludeVirtualCollections(bool exclude)
Sets whether we want virtual collections to be filtered or not.
Represents a collection of PIM items.
QStringList contentMimeTypes() const
Returns a list of possible content mimetypes, e.g.
static Collection root()
Returns the root collection.
Rights rights() const
Returns the rights the user has on the collection.
QList< Collection > List
Describes a list of collections.
@ CanCreateItem
Can create new items in this collection.
@ CanCreateCollection
Can create new subcollections in this collection.
bool isVirtual() const
Returns whether the collection is virtual, for example a search collection.
A proxy model that filters entities by access rights.
A model for collections and items together.
@ NoItemPopulation
Do not include items in the model.
void setItemPopulationStrategy(ItemPopulationStrategy strategy)
Sets the item population strategy of the model.
@ CollectionRole
The collection.
A view to show an item/collection tree provided by an EntityTreeModel.
virtual void setModel(QAbstractItemModel *model)
bool isValid() const
Returns whether the entity is valid.
void setCollectionMonitored(const Collection &collection, bool monitored=true)
Sets whether the specified collection shall be monitored for changes.
void fetchCollection(bool enable)
Enables automatic fetching of changed collections from the Akonadi storage.
FreeBusyManager::Singleton.