File indexing completed on 2024-11-10 04:40:28

0001 /*
0002     SPDX-FileCopyrightText: 2006 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "collectioncreatejob.h"
0008 #include "job_p.h"
0009 #include "private/protocol_p.h"
0010 #include "protocolhelper_p.h"
0011 
0012 #include <KLocalizedString>
0013 
0014 using namespace Akonadi;
0015 
0016 class Akonadi::CollectionCreateJobPrivate : public JobPrivate
0017 {
0018 public:
0019     explicit CollectionCreateJobPrivate(CollectionCreateJob *parent)
0020         : JobPrivate(parent)
0021     {
0022     }
0023 
0024     QString jobDebuggingString() const override;
0025     Collection mCollection;
0026 };
0027 
0028 QString Akonadi::CollectionCreateJobPrivate::jobDebuggingString() const
0029 {
0030     return QStringLiteral("Create collection: %1").arg(mCollection.id());
0031 }
0032 
0033 CollectionCreateJob::CollectionCreateJob(const Collection &collection, QObject *parent)
0034     : Job(new CollectionCreateJobPrivate(this), parent)
0035 {
0036     Q_D(CollectionCreateJob);
0037 
0038     d->mCollection = collection;
0039 }
0040 
0041 CollectionCreateJob::~CollectionCreateJob()
0042 {
0043 }
0044 
0045 void CollectionCreateJob::doStart()
0046 {
0047     Q_D(CollectionCreateJob);
0048     if (d->mCollection.parentCollection().id() < 0 && d->mCollection.parentCollection().remoteId().isEmpty()) {
0049         setError(Unknown);
0050         setErrorText(i18n("Invalid parent"));
0051         emitResult();
0052         return;
0053     }
0054 
0055     auto cmd = Protocol::CreateCollectionCommandPtr::create();
0056     cmd->setName(d->mCollection.name());
0057     cmd->setParent(ProtocolHelper::entityToScope(d->mCollection.parentCollection()));
0058     cmd->setMimeTypes(d->mCollection.contentMimeTypes());
0059     cmd->setRemoteId(d->mCollection.remoteId());
0060     cmd->setRemoteRevision(d->mCollection.remoteRevision());
0061     cmd->setIsVirtual(d->mCollection.isVirtual());
0062     cmd->setEnabled(d->mCollection.enabled());
0063     cmd->setDisplayPref(ProtocolHelper::listPreference(d->mCollection.localListPreference(Collection::ListDisplay)));
0064     cmd->setSyncPref(ProtocolHelper::listPreference(d->mCollection.localListPreference(Collection::ListSync)));
0065     cmd->setIndexPref(ProtocolHelper::listPreference(d->mCollection.localListPreference(Collection::ListIndex)));
0066     cmd->setCachePolicy(ProtocolHelper::cachePolicyToProtocol(d->mCollection.cachePolicy()));
0067     Protocol::Attributes attrs;
0068     const Akonadi::Attribute::List attrList = d->mCollection.attributes();
0069     for (Attribute *attr : attrList) {
0070         attrs.insert(attr->type(), attr->serialized());
0071     }
0072     cmd->setAttributes(attrs);
0073 
0074     d->sendCommand(cmd);
0075     emitWriteFinished();
0076 }
0077 
0078 Collection CollectionCreateJob::collection() const
0079 {
0080     Q_D(const CollectionCreateJob);
0081 
0082     return d->mCollection;
0083 }
0084 
0085 bool CollectionCreateJob::doHandleResponse(qint64 tag, const Protocol::CommandPtr &response)
0086 {
0087     Q_D(CollectionCreateJob);
0088 
0089     if (!response->isResponse()) {
0090         return Job::doHandleResponse(tag, response);
0091     }
0092 
0093     if (response->type() == Protocol::Command::FetchCollections) {
0094         const auto &resp = Protocol::cmdCast<Protocol::FetchCollectionsResponse>(response);
0095         Collection col = ProtocolHelper::parseCollection(resp);
0096         if (!col.isValid()) {
0097             setError(Unknown);
0098             setErrorText(i18n("Failed to parse Collection from response"));
0099             return true;
0100         }
0101         col.setParentCollection(d->mCollection.parentCollection());
0102         col.setName(d->mCollection.name());
0103         col.setRemoteId(d->mCollection.remoteId());
0104         col.setRemoteRevision(d->mCollection.remoteRevision());
0105         col.setVirtual(d->mCollection.isVirtual());
0106         d->mCollection = col;
0107         return false;
0108     }
0109 
0110     if (response->type() == Protocol::Command::CreateCollection) {
0111         return true;
0112     }
0113 
0114     return Job::doHandleResponse(tag, response);
0115 }
0116 
0117 #include "moc_collectioncreatejob.cpp"