6#include "collectioncreatehandler.h"
9#include "handlerhelper.h"
10#include "shared/akranges.h"
11#include "storage/datastore.h"
12#include "storage/selectquerybuilder.h"
13#include "storage/transaction.h"
15#include "private/scope_p.h"
18using namespace Akonadi::Server;
19using namespace AkRanges;
24[[nodiscard]]
bool isTopLevelCollection(
const Scope &scope)
26 return scope.scope() == Scope::Uid && scope.uidSet().size() == 1 && scope.uid() == 0;
31CollectionCreateHandler::CollectionCreateHandler(AkonadiServer &akonadi)
36bool CollectionCreateHandler::parseStream()
38 const auto &cmd = Protocol::cmdCast<Protocol::CreateCollectionCommand>(m_command);
40 if (cmd.name().isEmpty()) {
41 return failureResponse(QStringLiteral(
"Invalid collection name"));
45 qint64 resourceId = 0;
46 bool forceVirtual =
false;
47 MimeType::List parentContentTypes;
50 if (!isTopLevelCollection(cmd.parent())) {
51 parent = HandlerHelper::collectionFromScope(cmd.parent(), connection()->context());
52 if (!parent.isValid()) {
53 return failureResponse(QStringLiteral(
"Invalid parent collection"));
57 parentContentTypes = parent.mimeTypes();
58 const auto hasMimeType = [](
const QString &
mimeType) {
59 return [
mimeType](
const MimeType &mt) {
63 const bool canContainCollections = parentContentTypes | Actions::any(hasMimeType(CollectionMimeType));
64 const bool canContainVirtualCollections = parentContentTypes | Actions::any(hasMimeType(VirtualCollectionMimeType));
66 if (!canContainCollections && !canContainVirtualCollections) {
67 return failureResponse(QStringLiteral(
"Parent collection can not contain sub-collections"));
72 if (canContainVirtualCollections && !canContainCollections) {
77 resourceId = parent.resourceId();
80 Resource res = Resource::retrieveByName(sessionId);
82 return failureResponse(QStringLiteral(
"Cannot create top-level collection"));
84 resourceId = res.id();
87 Collection collection;
88 if (parent.isValid()) {
89 collection.setParentId(parent.id());
92 collection.setResourceId(resourceId);
95 collection.setIsVirtual(cmd.isVirtual() || forceVirtual);
97 collection.setSyncPref(
static_cast<Collection::Tristate
>(cmd.syncPref()));
98 collection.setDisplayPref(
static_cast<Collection::Tristate
>(cmd.displayPref()));
99 collection.setIndexPref(
static_cast<Collection::Tristate
>(cmd.indexPref()));
100 const Protocol::CachePolicy &cp = cmd.
cachePolicy();
101 collection.setCachePolicyCacheTimeout(cp.cacheTimeout());
102 collection.setCachePolicyCheckInterval(cp.checkInterval());
103 collection.setCachePolicyInherit(cp.inherit());
104 collection.setCachePolicyLocalParts(cp.localParts().join(QLatin1Char(
' ')));
105 collection.setCachePolicySyncOnDemand(cp.syncOnDemand());
107 DataStore *db = connection()->storageBackend();
108 Transaction transaction(db, QStringLiteral(
"CREATE"));
110 QStringList effectiveMimeTypes = cmd.mimeTypes();
111 if (effectiveMimeTypes.
isEmpty()) {
112 effectiveMimeTypes = parentContentTypes | Views::transform(&MimeType::name) | Actions::toQList;
115 if (!db->appendCollection(collection, effectiveMimeTypes, cmd.attributes())) {
116 return failureResponse(QStringLiteral(
"Could not create collection %1, resourceId %2").arg(cmd.name()).arg(resourceId));
119 if (!transaction.commit()) {
120 return failureResponse(QStringLiteral(
"Unable to commit transaction."));
127 return successResponse<Protocol::CreateCollectionResponse>();
void setName(const QString &name)
Sets the i18n'ed name of the collection.
CachePolicy cachePolicy() const
Returns the cache policy of the collection.
void setRemoteId(const QString &id)
Sets the remote id of the collection.
void setEnabled(bool enabled)
Sets the collection's enabled state.
void setRemoteRevision(const QString &revision)
Sets the remote revision of the collection.
virtual void activeCachePolicy(Collection &col)
Determines the active cache policy for this Collection.
static Protocol::FetchCollectionsResponse fetchCollectionsResponse(AkonadiServer &akonadi, const Collection &col)
Returns the protocol representation of the given collection.
The handler interfaces describes an entity capable of handling an AkonadiIMAP command.
Helper integration between Akonadi and Qt.
KCALUTILS_EXPORT QString mimeType()
bool isEmpty() const const
QString fromUtf8(QByteArrayView str)