1/*
2 Copyright (c) 2006 Volker Krause <vkrause@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 "collectionmodifyjob.h"
21
22#include "changemediator_p.h"
23#include "collection_p.h"
24#include "collectionstatistics.h"
25#include "imapparser_p.h"
26#include "job_p.h"
27#include "protocolhelper_p.h"
28
29#include <akonadi/private/protocol_p.h>
30
31using namespace Akonadi;
32
33class Akonadi::CollectionModifyJobPrivate : public JobPrivate
34{
35public:
36 CollectionModifyJobPrivate(CollectionModifyJob *parent)
37 : JobPrivate(parent)
38 {
39 }
40
41 virtual QString jobDebuggingString() const
42 {
43 return QString::fromLatin1("Collection Id %1").arg(mCollection.id());
44 }
45
46 Collection mCollection;
47};
48
49CollectionModifyJob::CollectionModifyJob(const Collection &collection, QObject *parent)
50 : Job(new CollectionModifyJobPrivate(this), parent)
51{
52 Q_D(CollectionModifyJob);
53 d->mCollection = collection;
54}
55
56CollectionModifyJob::~CollectionModifyJob()
57{
58}
59
60void CollectionModifyJob::doStart()
61{
62 Q_D(CollectionModifyJob);
63 QByteArray command = d->newTag();
64 try {
65 command += ProtocolHelper::entityIdToByteArray(d->mCollection, AKONADI_CMD_COLLECTIONMODIFY);
66 } catch (const std::exception &e) {
67 setError(Job::Unknown);
68 setErrorText(QString::fromUtf8(e.what()));
69 emitResult();
70 return;
71 }
72
73 QByteArray changes;
74 if (d->mCollection.d_func()->contentTypesChanged) {
75 QList<QByteArray> bList;
76 foreach (const QString &s, d->mCollection.contentMimeTypes()) {
77 bList << s.toLatin1();
78 }
79 changes += " MIMETYPE (" + ImapParser::join(bList, " ") + ')';
80 }
81 if (d->mCollection.parentCollection().id() >= 0) {
82 changes += " PARENT " + QByteArray::number(d->mCollection.parentCollection().id());
83 }
84 if (!d->mCollection.name().isEmpty()) {
85 changes += " NAME " + ImapParser::quote(d->mCollection.name().toUtf8());
86 }
87 if (!d->mCollection.remoteId().isNull()) {
88 changes += " REMOTEID " + ImapParser::quote(d->mCollection.remoteId().toUtf8());
89 }
90 if (!d->mCollection.remoteRevision().isNull()) {
91 changes += " REMOTEREVISION " + ImapParser::quote(d->mCollection.remoteRevision().toUtf8());
92 }
93 if (d->mCollection.d_func()->cachePolicyChanged) {
94 changes += ' ' + ProtocolHelper::cachePolicyToByteArray(d->mCollection.cachePolicy());
95 }
96 if (d->mCollection.d_func()->enabledChanged) {
97 changes += ' ' + ProtocolHelper::enabled(d->mCollection.enabled());
98 }
99 if (d->mCollection.d_func()->listPreferenceChanged) {
100 changes += ' ' + ProtocolHelper::listPreference(Collection::ListDisplay, d->mCollection.localListPreference(Collection::ListDisplay));
101 changes += ' ' + ProtocolHelper::listPreference(Collection::ListSync, d->mCollection.localListPreference(Collection::ListSync));
102 changes += ' ' + ProtocolHelper::listPreference(Collection::ListIndex, d->mCollection.localListPreference(Collection::ListIndex));
103 }
104 if (d->mCollection.d_func()->referencedChanged) {
105 changes += ' ' + ProtocolHelper::referenced(d->mCollection.referenced());
106 }
107 if (d->mCollection.attributes().count() > 0) {
108 changes += ' ' + ProtocolHelper::attributesToByteArray(d->mCollection);
109 }
110 foreach (const QByteArray &b, d->mCollection.d_func()->mDeletedAttributes) {
111 changes += " -" + b;
112 }
113 if (changes.isEmpty()) {
114 emitResult();
115 return;
116 }
117 command += changes + '\n';
118 d->writeData(command);
119
120 ChangeMediator::invalidateCollection(d->mCollection);
121}
122
123Collection CollectionModifyJob::collection() const
124{
125 const Q_D(CollectionModifyJob);
126 return d->mCollection;
127}
128