Akonadi

collectioncreatejob.cpp
1 /*
2  SPDX-FileCopyrightText: 2006 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "collectioncreatejob.h"
8 #include "protocolhelper_p.h"
9 #include "job_p.h"
10 #include "private/protocol_p.h"
11 
12 #include <KLocalizedString>
13 
14 using namespace Akonadi;
15 
16 class Akonadi::CollectionCreateJobPrivate : public JobPrivate
17 {
18 public:
19  explicit CollectionCreateJobPrivate(CollectionCreateJob *parent)
20  : JobPrivate(parent)
21  {
22  }
23 
24  QString jobDebuggingString() const override;
25  Collection mCollection;
26 
27 };
28 
29 QString Akonadi::CollectionCreateJobPrivate::jobDebuggingString() const
30 {
31  return QStringLiteral("Create collection: %1").arg(mCollection.id());
32 }
33 
35  : Job(new CollectionCreateJobPrivate(this), parent)
36 {
38 
39  d->mCollection = collection;
40 }
41 
43 {
44 }
45 
47 {
49  if (d->mCollection.parentCollection().id() < 0 && d->mCollection.parentCollection().remoteId().isEmpty()) {
51  setErrorText(i18n("Invalid parent"));
52  emitResult();
53  return;
54  }
55 
56  auto cmd = Protocol::CreateCollectionCommandPtr::create();
57  cmd->setName(d->mCollection.name());
58  cmd->setParent(ProtocolHelper::entityToScope(d->mCollection.parentCollection()));
59  cmd->setMimeTypes(d->mCollection.contentMimeTypes());
60  cmd->setRemoteId(d->mCollection.remoteId());
61  cmd->setRemoteRevision(d->mCollection.remoteRevision());
62  cmd->setIsVirtual(d->mCollection.isVirtual());
63  cmd->setEnabled(d->mCollection.enabled());
64  cmd->setDisplayPref(ProtocolHelper::listPreference(d->mCollection.localListPreference(Collection::ListDisplay)));
65  cmd->setSyncPref(ProtocolHelper::listPreference(d->mCollection.localListPreference(Collection::ListSync)));
66  cmd->setIndexPref(ProtocolHelper::listPreference(d->mCollection.localListPreference(Collection::ListIndex)));
67  cmd->setCachePolicy(ProtocolHelper::cachePolicyToProtocol(d->mCollection.cachePolicy()));
68  Protocol::Attributes attrs;
69  const Akonadi::Attribute::List attrList = d->mCollection.attributes();
70  for (Attribute *attr : attrList) {
71  attrs.insert(attr->type(), attr->serialized());
72  }
73  cmd->setAttributes(attrs);
74 
75  d->sendCommand(cmd);
77 }
78 
80 {
81  Q_D(const CollectionCreateJob);
82 
83  return d->mCollection;
84 }
85 
87 {
89 
90  if (!response->isResponse()) {
91  return Job::doHandleResponse(tag, response);
92  }
93 
94  if (response->type() == Protocol::Command::FetchCollections) {
95  const auto &resp = Protocol::cmdCast<Protocol::FetchCollectionsResponse>(response);
96  Collection col = ProtocolHelper::parseCollection(resp);
97  if (!col.isValid()) {
99  setErrorText(i18n("Failed to parse Collection from response"));
100  return true;
101  }
102  col.setParentCollection(d->mCollection.parentCollection());
103  col.setName(d->mCollection.name());
104  col.setRemoteId(d->mCollection.remoteId());
105  col.setRemoteRevision(d->mCollection.remoteRevision());
106  col.setVirtual(d->mCollection.isVirtual());
107  d->mCollection = col;
108  return false;
109  }
110 
111  if (response->type() == Protocol::Command::CreateCollection) {
112  return true;
113  }
114 
115  return Job::doHandleResponse(tag, response);
116 }
virtual bool doHandleResponse(qint64 tag, const Protocol::CommandPtr &response)
This method should be reimplemented in the concrete jobs in case you want to handle incoming data...
Definition: job.cpp:372
void emitResult()
Unknown error.
Definition: job.h:102
CollectionCreateJob(const Collection &collection, QObject *parent=nullptr)
Creates a new collection create job.
Represents a collection of PIM items.
Definition: collection.h:63
void setError(int errorCode)
Listing for display to the user.
Definition: collection.h:470
Base class for all actions in the Akonadi storage.
Definition: job.h:80
Listing for indexing the content.
Definition: collection.h:471
Provides interface for custom attributes for Entity.
Definition: attribute.h:126
void setErrorText(const QString &errorText)
~CollectionCreateJob() override
Destroys the collection create job.
void emitWriteFinished()
Call this method to indicate that this job will not call writeData() again.
Definition: job.cpp:398
QString i18n(const char *text, const TYPE &arg...)
void insert(int i, const T &value)
Helper integration between Akonadi and Qt.
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
void doStart() override
This method must be reimplemented in the concrete jobs.
Job that creates a new collection in the Akonadi storage.
bool doHandleResponse(qint64 tag, const Protocol::CommandPtr &response) override
This method should be reimplemented in the concrete jobs in case you want to handle incoming data...
Listing for synchronization.
Definition: collection.h:469
Collection collection() const
Returns the created collection if the job was executed successfully.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed Jul 8 2020 23:15:02 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.