Warning: That file was not part of the compilation database. It may have many parsing errors.
1 | /* |
---|---|
2 | Copyright 2010 Tobias Koenig <tokoe@kde.org> |
3 | |
4 | This library is free software; you can redistribute it and/or modify it |
5 | under the terms of the GNU Library General Public License as published by |
6 | the Free Software Foundation; either version 2 of the License, or (at your |
7 | option) any later version. |
8 | |
9 | This library is distributed in the hope that it will be useful, but WITHOUT |
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public |
12 | License for more details. |
13 | |
14 | You should have received a copy of the GNU Library General Public License |
15 | along with this library; see the file COPYING.LIB. If not, write to the |
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
17 | 02110-1301, USA. |
18 | */ |
19 | |
20 | #include "collectiondialog_mobile_p.h" |
21 | #include "asyncselectionhandler_p.h" |
22 | #include "collectiondialog.h" |
23 | |
24 | #include <qplatformdefs.h> |
25 | |
26 | #include <kdescendantsproxymodel.h> |
27 | |
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> |
34 | |
35 | #include <KLocalizedString> |
36 | #include <KInputDialog> |
37 | #include <KUrl> |
38 | #include <KMessageBox> |
39 | #include <KStandardDirs> |
40 | |
41 | #include <QDeclarativeView> |
42 | |
43 | using namespace Akonadi; |
44 | |
45 | CollectionDialog::Private::Private(QAbstractItemModel *customModel, CollectionDialog *parent, CollectionDialogOptions options) |
46 | : QObject(parent) |
47 | , mParent(parent) |
48 | , mSelectionMode(QAbstractItemView::SingleSelection) |
49 | , mOkButtonEnabled(false) |
50 | , mCancelButtonEnabled(true) |
51 | , mCreateButtonEnabled(false) |
52 | { |
53 | // setup GUI |
54 | mView = new QDeclarativeView(mParent); |
55 | mView->setResizeMode(QDeclarativeView::SizeRootObjectToView); |
56 | |
57 | mParent->setMainWidget(mView); |
58 | mParent->setButtons(KDialog::None); |
59 | |
60 | changeCollectionDialogOptions(options); |
61 | |
62 | QAbstractItemModel *baseModel; |
63 | |
64 | if (customModel) { |
65 | baseModel = customModel; |
66 | } else { |
67 | mMonitor = new Akonadi::ChangeRecorder(mParent); |
68 | mMonitor->fetchCollection(true); |
69 | mMonitor->setCollectionMonitored(Akonadi::Collection::root()); |
70 | |
71 | mModel = new EntityTreeModel(mMonitor, mParent); |
72 | mModel->setItemPopulationStrategy(EntityTreeModel::NoItemPopulation); |
73 | |
74 | baseModel = mModel; |
75 | } |
76 | |
77 | KDescendantsProxyModel *proxyModel = new KDescendantsProxyModel(parent); |
78 | proxyModel->setDisplayAncestorData(true); |
79 | proxyModel->setSourceModel(baseModel); |
80 | |
81 | mMimeTypeFilterModel = new CollectionFilterProxyModel(parent); |
82 | mMimeTypeFilterModel->setSourceModel(proxyModel); |
83 | |
84 | mRightsFilterModel = new EntityRightsFilterModel(parent); |
85 | mRightsFilterModel->setSourceModel(mMimeTypeFilterModel); |
86 | |
87 | mFilterModel = new QSortFilterProxyModel(parent); |
88 | mFilterModel->setFilterCaseSensitivity(Qt::CaseInsensitive); |
89 | mFilterModel->setSourceModel(mRightsFilterModel); |
90 | |
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))); |
96 | |
97 | mSelectionHandler = new AsyncSelectionHandler(mFilterModel, mParent); |
98 | mParent->connect(mSelectionHandler, SIGNAL(collectionAvailable(QModelIndex)), |
99 | SLOT(slotCollectionAvailable(QModelIndex))); |
100 | |
101 | foreach (const QString &importPath, KGlobal::dirs()->findDirs("module", QLatin1String( "imports"))) { |
102 | mView->engine()->addImportPath(importPath); |
103 | } |
104 | |
105 | mView->rootContext()->setContextProperty(QLatin1String("dialogController"), this); |
106 | mView->rootContext()->setContextProperty(QLatin1String("collectionModel"), mFilterModel); |
107 | |
108 | // QUICKHACK: since we have no KDE integration plugin available in kdelibs, we have to do the translation in C++ space |
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( |
112 | |
113 | mView->setSource(KUrl::fromLocalFile(KStandardDirs::locate("data", QLatin1String( "akonadi-kde/qml/CollectionDialogMobile.qml")))); |
114 | |
115 | #if defined (Q_WS_MAEMO_5) || defined (MEEGO_EDITION_HARMATTAN) |
116 | mParent->setWindowState(Qt::WindowFullScreen); |
117 | #else |
118 | // on the desktop start with a nice size |
119 | mParent->resize(800, 480); |
120 | #endif |
121 | } |
122 | |
123 | CollectionDialog::Private::~Private() |
124 | { |
125 | } |
126 | |
127 | void CollectionDialog::Private::slotCollectionAvailable(const QModelIndex &index) |
128 | { |
129 | mSelectionModel->setCurrentIndex(index, QItemSelectionModel::ClearAndSelect); |
130 | } |
131 | |
132 | void CollectionDialog::Private::slotFilterFixedString(const QString &filter) |
133 | { |
134 | } |
135 | |
136 | void CollectionDialog::Private::slotSelectionChanged() |
137 | { |
138 | mOkButtonEnabled = mSelectionModel->hasSelection(); |
139 | if (mAllowToCreateNewChildCollection) { |
140 | const Akonadi::Collection parentCollection = mParent->selectedCollection(); |
141 | const bool canCreateChildCollections = canCreateCollection(parentCollection); |
142 | const bool isVirtual = parentCollection.isVirtual(); |
143 | |
144 | mCreateButtonEnabled = (canCreateChildCollections && !isVirtual); |
145 | if (parentCollection.isValid()) { |
146 | const bool canCreateItems = (parentCollection.rights() & Akonadi::Collection::CanCreateItem); |
147 | mOkButtonEnabled = canCreateItems; |
148 | } |
149 | } |
150 | |
151 | emit buttonStatusChanged(); |
152 | } |
153 | |
154 | void CollectionDialog::Private::changeCollectionDialogOptions(CollectionDialogOptions options) |
155 | { |
156 | mAllowToCreateNewChildCollection = (options & AllowToCreateNewChildCollection); |
157 | emit buttonStatusChanged(); |
158 | } |
159 | |
160 | bool CollectionDialog::Private::canCreateCollection(const Akonadi::Collection &parentCollection) const |
161 | { |
162 | if (!parentCollection.isValid()) { |
163 | return false; |
164 | } |
165 | |
166 | if ((parentCollection.rights() & Akonadi::Collection::CanCreateCollection)) { |
167 | const QStringList dialogMimeTypeFilter = mParent->mimeTypeFilter(); |
168 | const QStringList parentCollectionMimeTypes = parentCollection.contentMimeTypes(); |
169 | Q_FOREACH (const QString &mimetype, dialogMimeTypeFilter) { |
170 | if (parentCollectionMimeTypes.contains(mimetype)) { |
171 | return true; |
172 | } |
173 | } |
174 | return true; |
175 | } |
176 | return false; |
177 | } |
178 | |
179 | void CollectionDialog::Private::slotAddChildCollection() |
180 | { |
181 | const Akonadi::Collection parentCollection = mParent->selectedCollection(); |
182 | if (canCreateCollection(parentCollection)) { |
183 | const QString name = KInputDialog::getText(i18nc("@title:window", "New Folder"), |
184 | i18nc("@label:textbox, name of a thing", "Name"), |
185 | QString(), 0, mParent); |
186 | if (name.isEmpty()) { |
187 | return; |
188 | } |
189 | |
190 | Akonadi::Collection collection; |
191 | collection.setName(name); |
192 | collection.setParentCollection(parentCollection); |
193 | Akonadi::CollectionCreateJob *job = new Akonadi::CollectionCreateJob(collection); |
194 | connect(job, SIGNAL(result(KJob*)), mParent, SLOT(slotCollectionCreationResult(KJob*))); |
195 | } |
196 | } |
197 | |
198 | void CollectionDialog::Private::slotCollectionCreationResult(KJob *job) |
199 | { |
200 | if (job->error()) { |
201 | KMessageBox::error(mParent, i18n("Could not create folder: %1", job->errorString()), |
202 | i18n("Folder creation failed")); |
203 | } |
204 | } |
205 | |
206 | void CollectionDialog::Private::setDescriptionText(const QString &text) |
207 | { |
208 | mDescriptionText = text; |
209 | emit descriptionTextChanged(); |
210 | } |
211 | |
212 | QString CollectionDialog::Private::descriptionText() const |
213 | { |
214 | return mDescriptionText; |
215 | } |
216 | |
217 | bool CollectionDialog::Private::okButtonEnabled() const |
218 | { |
219 | return mOkButtonEnabled; |
220 | } |
221 | |
222 | bool CollectionDialog::Private::cancelButtonEnabled() const |
223 | { |
224 | return mCancelButtonEnabled; |
225 | } |
226 | |
227 | bool CollectionDialog::Private::createButtonEnabled() const |
228 | { |
229 | return mCreateButtonEnabled; |
230 | } |
231 | |
232 | bool CollectionDialog::Private::createButtonVisible() const |
233 | { |
234 | return mAllowToCreateNewChildCollection; |
235 | } |
236 | |
237 | void CollectionDialog::Private::okClicked() |
238 | { |
239 | mParent->accept(); |
240 | } |
241 | |
242 | void CollectionDialog::Private::cancelClicked() |
243 | { |
244 | mParent->reject(); |
245 | } |
246 | |
247 | void CollectionDialog::Private::createClicked() |
248 | { |
249 | slotAddChildCollection(); |
250 | } |
251 | |
252 | void CollectionDialog::Private::setCurrentIndex(int row) |
253 | { |
254 | const QModelIndex index = mSelectionModel->model()->index(row, 0); |
255 | mSelectionModel->select(index, QItemSelectionModel::ClearAndSelect); |
256 | } |
257 | |
258 | void CollectionDialog::Private::setFilterText(const QString &text) |
259 | { |
260 | mFilterModel->setFilterFixedString(text); |
261 | } |
262 | |
263 | void CollectionDialog::Private::selectionChanged(const QItemSelection &selection, const QItemSelection &) |
264 | { |
265 | if (selection.isEmpty()) { |
266 | return; |
267 | } |
268 | |
269 | emit selectionChanged(selection.indexes().first().row()); |
270 | } |
271 | |
272 | CollectionDialog::CollectionDialog(QWidget *parent) |
273 | : KDialog(parent, Qt::Window) |
274 | , d(new Private(0, this, CollectionDialog::None)) |
275 | { |
276 | } |
277 | |
278 | CollectionDialog::CollectionDialog(QAbstractItemModel *model, QWidget *parent) |
279 | : KDialog(parent, Qt::Window) |
280 | , d(new Private(model, this, CollectionDialog::None)) |
281 | { |
282 | } |
283 | |
284 | CollectionDialog::CollectionDialog(CollectionDialogOptions options, QAbstractItemModel *model, QWidget *parent) |
285 | : KDialog(parent, Qt::Window) |
286 | , d(new Private(model, this, options)) |
287 | { |
288 | } |
289 | |
290 | CollectionDialog::~CollectionDialog() |
291 | { |
292 | } |
293 | |
294 | Akonadi::Collection CollectionDialog::selectedCollection() const |
295 | { |
296 | if (!d->mSelectionModel->hasSelection()) { |
297 | return Akonadi::Collection(); |
298 | } |
299 | |
300 | return d->mSelectionModel->selectedRows().first().data(Akonadi::EntityTreeModel::CollectionRole).value<Akonadi::Collection>(); |
301 | } |
302 | |
303 | Akonadi::Collection::List CollectionDialog::selectedCollections() const |
304 | { |
305 | if (!d->mSelectionModel->hasSelection()) { |
306 | return Akonadi::Collection::List(); |
307 | } |
308 | |
309 | return (Akonadi::Collection::List() << d->mSelectionModel->selectedRows().first().data(Akonadi::EntityTreeModel::CollectionRole).value<Akonadi::Collection>()); |
310 | } |
311 | |
312 | void CollectionDialog::setMimeTypeFilter(const QStringList &mimeTypes) |
313 | { |
314 | d->mMimeTypeFilterModel->clearFilters(); |
315 | d->mMimeTypeFilterModel->addMimeTypeFilters(mimeTypes); |
316 | } |
317 | |
318 | QStringList CollectionDialog::mimeTypeFilter() const |
319 | { |
320 | return d->mMimeTypeFilterModel->mimeTypes(); |
321 | } |
322 | |
323 | void CollectionDialog::setAccessRightsFilter(Collection::Rights rights) |
324 | { |
325 | d->mRightsFilterModel->setAccessRights(rights); |
326 | } |
327 | |
328 | Akonadi::Collection::Rights CollectionDialog::accessRightsFilter() const |
329 | { |
330 | return d->mRightsFilterModel->accessRights(); |
331 | } |
332 | |
333 | void CollectionDialog::setDescription(const QString &text) |
334 | { |
335 | d->setDescriptionText(text); |
336 | } |
337 | |
338 | void CollectionDialog::setDefaultCollection(const Collection &collection) |
339 | { |
340 | d->mSelectionHandler->waitForCollection(collection); |
341 | } |
342 | |
343 | void CollectionDialog::setSelectionMode(QAbstractItemView::SelectionMode mode) |
344 | { |
345 | d->mSelectionMode = mode; |
346 | } |
347 | |
348 | QAbstractItemView::SelectionMode CollectionDialog::selectionMode() const |
349 | { |
350 | return d->mSelectionMode; |
351 | } |
352 | |
353 | void CollectionDialog::changeCollectionDialogOptions(CollectionDialogOptions options) |
354 | { |
355 | d->changeCollectionDialogOptions(options); |
356 | } |
357 | |
358 | #include "moc_collectiondialog.cpp" |
359 | #include "moc_collectiondialog_mobile_p.cpp" |
360 |
Warning: That file was not part of the compilation database. It may have many parsing errors.