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;
174 void slotDoubleClicked();
175 void slotSelectionChanged();
176 void slotAddChildCollection();
177 void slotCollectionCreationResult(KJob *job);
180 bool canSelectCollection()
const;
183 void CollectionDialog::Private::slotDoubleClicked()
185 if (canSelectCollection()) {
190 bool CollectionDialog::Private::canSelectCollection()
const 192 bool result = (mView->selectionModel()->selectedIndexes().count() > 0);
193 if (mAllowToCreateNewChildCollection) {
196 if (parentCollection.
isValid()) {
203 void CollectionDialog::Private::slotSelectionChanged()
205 mParent->enableButton(KDialog::Ok, mView->selectionModel()->selectedIndexes().count() > 0);
206 if (mAllowToCreateNewChildCollection) {
208 const bool canCreateChildCollections = canCreateCollection(parentCollection);
210 mParent->enableButton(KDialog::User1, (canCreateChildCollections && !parentCollection.
isVirtual()));
211 if (parentCollection.
isValid()) {
213 mParent->enableButton(KDialog::Ok, canCreateItems);
218 void CollectionDialog::Private::changeCollectionDialogOptions(CollectionDialogOptions options)
220 mAllowToCreateNewChildCollection = (options & AllowToCreateNewChildCollection);
221 if (mAllowToCreateNewChildCollection) {
222 mParent->setButtons(Ok | Cancel | User1);
223 mParent->setButtonGuiItem(User1, KGuiItem(i18n(
"&New Subfolder..."), QLatin1String(
"folder-new"),
224 i18n(
"Create a new subfolder under the currently selected folder")));
225 mParent->enableButton(KDialog::User1,
false);
226 connect(mParent, SIGNAL(user1Clicked()), mParent, SLOT(slotAddChildCollection()));
228 mKeepTreeExpanded = (options & KeepTreeExpanded);
229 if (mKeepTreeExpanded) {
230 mParent->connect(mRightsFilterModel, SIGNAL(rowsInserted(QModelIndex,
int,
int)),
231 mView, SLOT(expandAll()), Qt::UniqueConnection);
236 bool CollectionDialog::Private::canCreateCollection(
const Akonadi::Collection &parentCollection)
const 238 if (!parentCollection.
isValid()) {
243 const QStringList dialogMimeTypeFilter = mParent->mimeTypeFilter();
244 const QStringList parentCollectionMimeTypes = parentCollection.
contentMimeTypes();
245 Q_FOREACH (
const QString &mimetype, dialogMimeTypeFilter) {
246 if (parentCollectionMimeTypes.contains(mimetype)) {
255 void CollectionDialog::Private::slotAddChildCollection()
258 if (canCreateCollection(parentCollection)) {
259 const QString name = KInputDialog::getText(i18nc(
"@title:window",
"New Folder"),
260 i18nc(
"@label:textbox, name of a thing",
"Name"),
261 QString(), 0, mParent);
262 if (name.isEmpty()) {
270 connect(job, SIGNAL(result(KJob*)), mParent, SLOT(slotCollectionCreationResult(KJob*)));
274 void CollectionDialog::Private::slotCollectionCreationResult(KJob *job)
277 KMessageBox::error(mParent, i18n(
"Could not create folder: %1", job->errorString()),
278 i18n(
"Folder creation failed"));
296 , d(new Private(model, this, options))
308 const QModelIndex index = d->mView->currentIndex();
309 if (index.isValid()) {
320 const QItemSelectionModel *selectionModel = d->mView->selectionModel();
321 const QModelIndexList selectedIndexes = selectionModel->selectedIndexes();
322 foreach (
const QModelIndex &index, selectedIndexes) {
323 if (index.isValid()) {
326 collections.append(collection);
340 d->mMimeTypeFilterModel->clearFilters();
341 d->mMimeTypeFilterModel->addMimeTypeFilters(mimeTypes);
344 foreach (
const QString &mimetype, mimeTypes) {
345 d->mMonitor->setMimeTypeMonitored(mimetype);
352 return d->mMimeTypeFilterModel->mimeTypeFilters();
360 d->mRightsFilterModel->setAccessRights(rights);
365 return d->mRightsFilterModel->accessRights();
370 d->mTextLabel->setText(text);
371 d->mTextLabel->show();
376 d->mSelectionHandler->waitForCollection(collection);
381 d->mView->setSelectionMode(mode);
386 return d->mView->selectionMode();
391 d->changeCollectionDialogOptions(options);
396 d->mUseByDefault->setChecked(b);
397 d->mUseByDefault->show();
402 return d->mUseByDefault->isChecked();
405 #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 setParentCollection(const Collection &parent)
Set the parent collection of this object.
void setName(const QString &name)
Sets the i18n'ed name of the collection.
void setUseFolderByDefault(bool b)
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.