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>
35 #include <KLocalizedString>
36 #include <KInputDialog>
38 #include <KMessageBox>
39 #include <KStandardDirs>
41 #include <QDeclarativeView>
45 CollectionDialog::Private::Private(QAbstractItemModel *customModel,
CollectionDialog *parent, CollectionDialogOptions options)
48 , mSelectionMode(QAbstractItemView::SingleSelection)
49 , mOkButtonEnabled(false)
50 , mCancelButtonEnabled(true)
51 , mCreateButtonEnabled(false)
54 mView =
new QDeclarativeView(mParent);
55 mView->setResizeMode(QDeclarativeView::SizeRootObjectToView);
57 mParent->setMainWidget(mView);
58 mParent->setButtons(KDialog::None);
60 changeCollectionDialogOptions(options);
62 QAbstractItemModel *baseModel;
65 baseModel = customModel;
68 mMonitor->fetchCollection(
true);
72 mModel->setItemPopulationStrategy(EntityTreeModel::NoItemPopulation);
77 KDescendantsProxyModel *proxyModel =
new KDescendantsProxyModel(parent);
78 proxyModel->setDisplayAncestorData(
true);
79 proxyModel->setSourceModel(baseModel);
82 mMimeTypeFilterModel->setSourceModel(proxyModel);
85 mRightsFilterModel->setSourceModel(mMimeTypeFilterModel);
87 mFilterModel =
new QSortFilterProxyModel(parent);
88 mFilterModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
89 mFilterModel->setSourceModel(mRightsFilterModel);
91 mSelectionModel =
new QItemSelectionModel(mFilterModel);
92 mParent->connect(mSelectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
93 SLOT(slotSelectionChanged()));
94 mParent->connect(mSelectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
95 this, SLOT(selectionChanged(QItemSelection,QItemSelection)));
98 mParent->connect(mSelectionHandler, SIGNAL(collectionAvailable(QModelIndex)),
99 SLOT(slotCollectionAvailable(QModelIndex)));
101 foreach (
const QString &importPath, KGlobal::dirs()->findDirs(
"module", QLatin1String(
"imports"))) {
102 mView->engine()->addImportPath(importPath);
105 mView->rootContext()->setContextProperty(QLatin1String(
"dialogController"),
this);
106 mView->rootContext()->setContextProperty(QLatin1String(
"collectionModel"), mFilterModel);
109 mView->rootContext()->setContextProperty(QLatin1String(
"okButtonText"), KStandardGuiItem::ok().text().remove(QLatin1Char(
'&')));
110 mView->rootContext()->setContextProperty(QLatin1String(
"cancelButtonText"), KStandardGuiItem::cancel().text().remove(QLatin1Char(
'&')));
111 mView->rootContext()->setContextProperty(QLatin1String(
"createButtonText"), i18n(
"&New Subfolder...").remove(QLatin1Char(
'&')));
113 mView->setSource(KUrl::fromLocalFile(KStandardDirs::locate(
"data", QLatin1String(
"akonadi-kde/qml/CollectionDialogMobile.qml"))));
115 #if defined (Q_WS_MAEMO_5) || defined (MEEGO_EDITION_HARMATTAN)
116 mParent->setWindowState(Qt::WindowFullScreen);
119 mParent->resize(800, 480);
123 CollectionDialog::Private::~Private()
127 void CollectionDialog::Private::slotDoubleClicked()
131 void CollectionDialog::Private::slotCollectionAvailable(
const QModelIndex &index)
133 mSelectionModel->setCurrentIndex(index, QItemSelectionModel::ClearAndSelect);
136 void CollectionDialog::Private::slotFilterFixedString(
const QString &filter)
140 void CollectionDialog::Private::slotSelectionChanged()
142 mOkButtonEnabled = mSelectionModel->hasSelection();
143 if (mAllowToCreateNewChildCollection) {
145 const bool canCreateChildCollections = canCreateCollection(parentCollection);
146 const bool isVirtual = parentCollection.
isVirtual();
148 mCreateButtonEnabled = (canCreateChildCollections && !isVirtual);
149 if (parentCollection.
isValid()) {
151 mOkButtonEnabled = canCreateItems;
155 emit buttonStatusChanged();
158 void CollectionDialog::Private::changeCollectionDialogOptions(CollectionDialogOptions options)
160 mAllowToCreateNewChildCollection = (options & AllowToCreateNewChildCollection);
161 emit buttonStatusChanged();
164 bool CollectionDialog::Private::canCreateCollection(
const Akonadi::Collection &parentCollection)
const
166 if (!parentCollection.
isValid()) {
171 const QStringList dialogMimeTypeFilter = mParent->mimeTypeFilter();
172 const QStringList parentCollectionMimeTypes = parentCollection.
contentMimeTypes();
173 Q_FOREACH (
const QString &mimetype, dialogMimeTypeFilter) {
174 if (parentCollectionMimeTypes.contains(mimetype)) {
183 void CollectionDialog::Private::slotAddChildCollection()
186 if (canCreateCollection(parentCollection)) {
187 const QString name = KInputDialog::getText(i18nc(
"@title:window",
"New Folder"),
188 i18nc(
"@label:textbox, name of a thing",
"Name"),
189 QString(), 0, mParent);
190 if (name.isEmpty()) {
195 collection.setName(name);
196 collection.setParentCollection(parentCollection);
198 connect(job, SIGNAL(result(KJob*)), mParent, SLOT(slotCollectionCreationResult(KJob*)));
202 void CollectionDialog::Private::slotCollectionCreationResult(KJob *job)
205 KMessageBox::error(mParent, i18n(
"Could not create folder: %1", job->errorString()),
206 i18n(
"Folder creation failed"));
210 void CollectionDialog::Private::setDescriptionText(
const QString &text)
212 mDescriptionText = text;
213 emit descriptionTextChanged();
216 QString CollectionDialog::Private::descriptionText()
const
218 return mDescriptionText;
221 bool CollectionDialog::Private::okButtonEnabled()
const
223 return mOkButtonEnabled;
226 bool CollectionDialog::Private::cancelButtonEnabled()
const
228 return mCancelButtonEnabled;
231 bool CollectionDialog::Private::createButtonEnabled()
const
233 return mCreateButtonEnabled;
236 bool CollectionDialog::Private::createButtonVisible()
const
238 return mAllowToCreateNewChildCollection;
241 void CollectionDialog::Private::okClicked()
246 void CollectionDialog::Private::cancelClicked()
251 void CollectionDialog::Private::createClicked()
253 slotAddChildCollection();
256 void CollectionDialog::Private::setCurrentIndex(
int row)
258 const QModelIndex index = mSelectionModel->model()->index(row, 0);
259 mSelectionModel->select(index, QItemSelectionModel::ClearAndSelect);
262 void CollectionDialog::Private::setFilterText(
const QString &text)
264 mFilterModel->setFilterFixedString(text);
267 void CollectionDialog::Private::selectionChanged(
const QItemSelection &selection,
const QItemSelection &)
269 if (selection.isEmpty()) {
273 emit selectionChanged(selection.indexes().first().row());
276 CollectionDialog::CollectionDialog(QWidget *parent)
277 : KDialog(parent, Qt::Window)
282 CollectionDialog::CollectionDialog(QAbstractItemModel *model, QWidget *parent)
283 : KDialog(parent, Qt::Window)
288 CollectionDialog::CollectionDialog(CollectionDialogOptions options, QAbstractItemModel *model, QWidget *parent)
289 : KDialog(parent, Qt::Window)
290 , d(new Private(model, this, options))
294 CollectionDialog::~CollectionDialog()
300 if (!d->mSelectionModel->hasSelection()) {
309 if (!d->mSelectionModel->hasSelection()) {
318 d->mMimeTypeFilterModel->clearFilters();
319 d->mMimeTypeFilterModel->addMimeTypeFilters(mimeTypes);
324 return d->mMimeTypeFilterModel->mimeTypes();
329 d->mRightsFilterModel->setAccessRights(rights);
334 return d->mRightsFilterModel->accessRights();
339 d->setDescriptionText(text);
344 d->mSelectionHandler->waitForCollection(collection);
349 d->mSelectionMode = mode;
354 return d->mSelectionMode;
359 d->changeCollectionDialogOptions(options);
362 #include "moc_collectiondialog.cpp"
363 #include "moc_collectiondialog_mobile_p.cpp"
Records and replays change notification.
Job that creates a new collection in the Akonadi storage.
A collection selection dialog.
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.
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.
A proxy model that filters collections by mime type.
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.
@ CollectionRole
The collection.
bool isValid() const
Returns whether the entity is valid.
FreeBusyManager::Singleton.