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 <KLocalizedString> 42 #include <KInputDialog> 43 #include <KMessageBox> 47 class 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;
87 mMonitor->fetchCollection(
true);
96 mMimeTypeFilterModel->setSourceModel(baseModel);
97 mMimeTypeFilterModel->setExcludeVirtualCollections(
true);
100 mRightsFilterModel->setSourceModel(mMimeTypeFilterModel);
102 mFilterCollection =
new KRecursiveFilterProxyModel(mParent);
103 mFilterCollection->setDynamicSortFilter(
true);
104 mFilterCollection->setSourceModel(mRightsFilterModel);
105 mFilterCollection->setFilterCaseSensitivity(Qt::CaseInsensitive);
106 mView->setModel(mFilterCollection);
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);
181 bool canSelectCollection()
const;
184 void CollectionDialog::Private::slotDoubleClicked()
186 if (canSelectCollection()) {
191 bool CollectionDialog::Private::canSelectCollection()
const 193 bool result = (mView->selectionModel()->selectedIndexes().count() > 0);
194 if (mAllowToCreateNewChildCollection) {
197 if (parentCollection.
isValid()) {
204 void 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);
219 void 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);
237 bool 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)) {
256 void 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*)));
278 void 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" void setAccessRightsFilter(Collection::Rights rights)
Sets the access rights that the listed collections shall match with.
CollectionDialog(QWidget *parent=0)
Creates a new collection dialog.
void setSelectionMode(QAbstractItemView::SelectionMode mode)
Sets the selection mode.
void setMimeTypeFilter(const QStringList &mimeTypes)
Sets the mime types any of which the selected collection(s) shall support.
Rights rights() const
Returns the rights the user has on the collection.
A proxy model that filters collections by mime type.
A collection selection dialog.
Represents a collection of PIM items.
Do not include items in the model.
Can create new subcollections in this collection.
A proxy model that filters entities by access rights.
void setUseFolderByDefault(bool b)
void setContentMimeTypes(const QStringList &mimetypes)
Allow to specify collection content mimetype when we create new one.
A view to show an item/collection tree provided by an EntityTreeModel.
QStringList mimeTypeFilter() const
Returns the mime types any of which the selected collection(s) shall support.
Can create new items in this collection.
static Collection root()
Returns the root collection.
Collection::Rights accessRightsFilter() const
Sets the access rights that the listed collections shall match with.
void setDefaultCollection(const Collection &collection)
Sets the collection that shall be selected by default.
void changeCollectionDialogOptions(CollectionDialogOptions options)
Change collection dialog options.
QAbstractItemView::SelectionMode selectionMode() const
Returns the selection mode.
bool useFolderByDefault() const
void setItemPopulationStrategy(ItemPopulationStrategy strategy)
Sets the item population strategy of the model.
Akonadi::Collection::List selectedCollections() const
Returns the list of selected collections.
void setDescription(const QString &text)
Sets the text that will be shown in the dialog.
bool isVirtual() const
Returns whether the collection is virtual, for example a search collection.
FreeBusyManager::Singleton.
A model for collections and items together.
QStringList contentMimeTypes() const
Returns a list of possible content mimetypes, e.g.
Akonadi::Collection selectedCollection() const
Returns the selected collection if the selection mode is QAbstractItemView::SingleSelection.
~CollectionDialog()
Destroys the collection dialog.
Job that creates a new collection in the Akonadi storage.
QList< Collection > List
Describes a list of collections.
Records and replays change notification.
bool isValid() const
Returns whether the entity is valid.