Changeset View
Changeset View
Standalone View
Standalone View
src/akonadi/akonadistorage.cpp
Show First 20 Lines • Show All 41 Lines • ▼ Show 20 Line(s) | |||||
42 | #include <AkonadiCore/TransactionSequence> | 42 | #include <AkonadiCore/TransactionSequence> | ||
43 | #include <AkonadiCore/TagCreateJob> | 43 | #include <AkonadiCore/TagCreateJob> | ||
44 | #include <AkonadiCore/TagDeleteJob> | 44 | #include <AkonadiCore/TagDeleteJob> | ||
45 | #include <AkonadiCore/TagFetchJob> | 45 | #include <AkonadiCore/TagFetchJob> | ||
46 | #include <AkonadiCore/TagFetchScope> | 46 | #include <AkonadiCore/TagFetchScope> | ||
47 | #include <AkonadiCore/TagModifyJob> | 47 | #include <AkonadiCore/TagModifyJob> | ||
48 | #include <AkonadiCore/TagAttribute> | 48 | #include <AkonadiCore/TagAttribute> | ||
49 | #include "akonadi/akonadicollectionfetchjobinterface.h" | 49 | #include "akonadi/akonadicollectionfetchjobinterface.h" | ||
50 | #include "akonadi/akonadicollectionsearchjobinterface.h" | | |||
51 | #include "akonadi/akonadiitemfetchjobinterface.h" | 50 | #include "akonadi/akonadiitemfetchjobinterface.h" | ||
52 | #include "akonadi/akonaditagfetchjobinterface.h" | 51 | #include "akonadi/akonaditagfetchjobinterface.h" | ||
53 | #include "akonadi/akonadistoragesettings.h" | 52 | #include "akonadi/akonadistoragesettings.h" | ||
54 | 53 | | |||
55 | using namespace Akonadi; | 54 | using namespace Akonadi; | ||
56 | 55 | | |||
57 | class CollectionJob : public CollectionFetchJob, public CollectionFetchJobInterface | 56 | class CollectionJob : public CollectionFetchJob, public CollectionFetchJobInterface | ||
58 | { | 57 | { | ||
▲ Show 20 Lines • Show All 55 Lines • ▼ Show 20 Line(s) | 68 | { | |||
114 | return collections; | 113 | return collections; | ||
115 | } | 114 | } | ||
116 | 115 | | |||
117 | void setResource(const QString &resource) Q_DECL_OVERRIDE | 116 | void setResource(const QString &resource) Q_DECL_OVERRIDE | ||
118 | { | 117 | { | ||
119 | fetchScope().setResource(resource); | 118 | fetchScope().setResource(resource); | ||
120 | } | 119 | } | ||
121 | 120 | | |||
122 | void setFiltered(bool filter) Q_DECL_OVERRIDE | | |||
123 | { | | |||
124 | fetchScope().setListFilter(filter ? Akonadi::CollectionFetchScope::Display | | |||
125 | : Akonadi::CollectionFetchScope::NoFilter); | | |||
126 | } | | |||
127 | | ||||
128 | private: | 121 | private: | ||
129 | const Collection m_collection; | 122 | const Collection m_collection; | ||
130 | const Type m_type; | 123 | const Type m_type; | ||
131 | }; | 124 | }; | ||
132 | 125 | | |||
133 | class CollectionSearchJob : public CollectionFetchJob, public CollectionSearchJobInterface | | |||
134 | { | | |||
135 | Q_OBJECT | | |||
136 | public: | | |||
137 | CollectionSearchJob(const QString &collectionName, QObject *parent = Q_NULLPTR) | | |||
138 | : CollectionFetchJob(Akonadi::Collection::root(), | | |||
139 | CollectionJob::Recursive, | | |||
140 | parent), | | |||
141 | m_collectionName(collectionName) | | |||
142 | { | | |||
143 | } | | |||
144 | | ||||
145 | Collection::List collections() const Q_DECL_OVERRIDE | | |||
146 | { | | |||
147 | auto collections = CollectionFetchJob::collections(); | | |||
148 | | ||||
149 | // Memorize them to reconstruct the ancestor chain later | | |||
150 | QMap<Collection::Id, Collection> collectionsMap; | | |||
151 | collectionsMap[Akonadi::Collection::root().id()] = Akonadi::Collection::root(); | | |||
152 | foreach (const auto &collection, collections) { | | |||
153 | collectionsMap[collection.id()] = collection; | | |||
154 | } | | |||
155 | | ||||
156 | // Why the hell isn't fetchScope() const and returning a reference??? | | |||
157 | auto self = const_cast<CollectionSearchJob*>(this); | | |||
158 | const auto allowedMimeTypes = self->fetchScope().contentMimeTypes().toSet(); | | |||
159 | | ||||
160 | collections.erase(std::remove_if(collections.begin(), collections.end(), | | |||
161 | [allowedMimeTypes, this] (const Collection &collection) { | | |||
162 | auto mimeTypes = collection.contentMimeTypes().toSet(); | | |||
163 | return mimeTypes.intersect(allowedMimeTypes).isEmpty() | | |||
164 | || !collection.displayName().contains(m_collectionName, Qt::CaseInsensitive); | | |||
165 | }), | | |||
166 | collections.end()); | | |||
167 | | ||||
168 | // Replace the dummy parents in the ancestor chain with proper ones | | |||
169 | // full of juicy data | | |||
170 | std::function<Collection(const Collection&)> reconstructAncestors = | | |||
171 | [collectionsMap, &reconstructAncestors, this] (const Collection &collection) -> Collection { | | |||
172 | Q_ASSERT(collection.isValid()); | | |||
173 | | ||||
174 | if (collection == Akonadi::Collection::root()) | | |||
175 | return collection; | | |||
176 | | ||||
177 | auto parent = collection.parentCollection(); | | |||
178 | auto reconstructedParent = reconstructAncestors(collectionsMap[parent.id()]); | | |||
179 | | ||||
180 | auto result = collection; | | |||
181 | result.setParentCollection(reconstructedParent); | | |||
182 | return result; | | |||
183 | }; | | |||
184 | | ||||
185 | std::transform(collections.begin(), collections.end(), | | |||
186 | collections.begin(), reconstructAncestors); | | |||
187 | | ||||
188 | return collections; | | |||
189 | } | | |||
190 | | ||||
191 | private: | | |||
192 | QString m_collectionName; | | |||
193 | }; | | |||
194 | | ||||
195 | class ItemJob : public ItemFetchJob, public ItemFetchJobInterface | 126 | class ItemJob : public ItemFetchJob, public ItemFetchJobInterface | ||
196 | { | 127 | { | ||
197 | Q_OBJECT | 128 | Q_OBJECT | ||
198 | public: | 129 | public: | ||
199 | using ItemFetchJob::ItemFetchJob; | 130 | using ItemFetchJob::ItemFetchJob; | ||
200 | 131 | | |||
201 | Item::List items() const Q_DECL_OVERRIDE { return ItemFetchJob::items(); } | 132 | Item::List items() const Q_DECL_OVERRIDE { return ItemFetchJob::items(); } | ||
202 | 133 | | |||
▲ Show 20 Lines • Show All 110 Lines • ▼ Show 20 Line(s) | 233 | { | |||
313 | scope.setIncludeStatistics(true); | 244 | scope.setIncludeStatistics(true); | ||
314 | scope.setAncestorRetrieval(CollectionFetchScope::All); | 245 | scope.setAncestorRetrieval(CollectionFetchScope::All); | ||
315 | scope.setListFilter(Akonadi::CollectionFetchScope::Display); | 246 | scope.setListFilter(Akonadi::CollectionFetchScope::Display); | ||
316 | job->setFetchScope(scope); | 247 | job->setFetchScope(scope); | ||
317 | 248 | | |||
318 | return job; | 249 | return job; | ||
319 | } | 250 | } | ||
320 | 251 | | |||
321 | CollectionSearchJobInterface *Storage::searchCollections(QString collectionName, FetchContentTypes types) | | |||
322 | { | | |||
323 | QStringList contentMimeTypes; | | |||
324 | if (types & Notes) | | |||
325 | contentMimeTypes << NoteUtils::noteMimeType(); | | |||
326 | if (types & Tasks) | | |||
327 | contentMimeTypes << KCalCore::Todo::todoMimeType(); | | |||
328 | | ||||
329 | auto job = new CollectionSearchJob(collectionName); | | |||
330 | | ||||
331 | auto scope = job->fetchScope(); | | |||
332 | scope.setContentMimeTypes(contentMimeTypes); | | |||
333 | scope.setIncludeStatistics(true); | | |||
334 | scope.setAncestorRetrieval(CollectionFetchScope::All); | | |||
335 | scope.setListFilter(Akonadi::CollectionFetchScope::NoFilter); | | |||
336 | job->setFetchScope(scope); | | |||
337 | | ||||
338 | return job; | | |||
339 | } | | |||
340 | | ||||
341 | | ||||
342 | ItemFetchJobInterface *Storage::fetchItems(Collection collection) | 252 | ItemFetchJobInterface *Storage::fetchItems(Collection collection) | ||
343 | { | 253 | { | ||
344 | auto job = new ItemJob(collection); | 254 | auto job = new ItemJob(collection); | ||
345 | 255 | | |||
346 | configureItemFetchJob(job); | 256 | configureItemFetchJob(job); | ||
347 | 257 | | |||
348 | return job; | 258 | return job; | ||
349 | } | 259 | } | ||
▲ Show 20 Lines • Show All 58 Lines • Show Last 20 Lines |