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 "collectioncreatejob.h"
21#include "imapparser_p.h"
22#include "protocolhelper_p.h"
23
24#include "job_p.h"
25
26#include <kdebug.h>
27#include <KLocalizedString>
28using namespace Akonadi;
29
30class Akonadi::CollectionCreateJobPrivate : public JobPrivate
31{
32public:
33 CollectionCreateJobPrivate(CollectionCreateJob *parent)
34 : JobPrivate(parent)
35 {
36 }
37
38 Collection mCollection;
39};
40
41CollectionCreateJob::CollectionCreateJob(const Collection &collection, QObject *parent)
42 : Job(new CollectionCreateJobPrivate(this), parent)
43{
44 Q_D(CollectionCreateJob);
45
46 d->mCollection = collection;
47}
48
49CollectionCreateJob::~CollectionCreateJob()
50{
51}
52
53void CollectionCreateJob::doStart()
54{
55 Q_D(CollectionCreateJob);
56 if (d->mCollection.parentCollection().id() < 0 && d->mCollection.parentCollection().remoteId().isEmpty()) {
57 setError(Unknown);
58 setErrorText(i18n("Invalid parent"));
59 emitResult();
60 return;
61 }
62
63 QByteArray command = d->newTag();
64 if (d->mCollection.parentCollection().id() < 0) {
65 command += " RID";
66 }
67 command += " CREATE " + ImapParser::quote(d->mCollection.name().toUtf8()) + ' ';
68 if (d->mCollection.parentCollection().id() >= 0) {
69 command += QByteArray::number(d->mCollection.parentCollection().id());
70 } else {
71 command += ImapParser::quote(d->mCollection.parentCollection().remoteId().toUtf8());
72 }
73 command += " (";
74 if (!d->mCollection.contentMimeTypes().isEmpty()) {
75 QList<QByteArray> cList;
76 foreach (const QString &s, d->mCollection.contentMimeTypes()) {
77 cList << s.toLatin1();
78 }
79 command += "MIMETYPE (" + ImapParser::join(cList, QByteArray(" ")) + ')';
80 }
81 command += " REMOTEID " + ImapParser::quote(d->mCollection.remoteId().toUtf8());
82 command += " REMOTEREVISION " + ImapParser::quote(d->mCollection.remoteRevision().toUtf8());
83 command += " VIRTUAL " + QByteArray::number(d->mCollection.isVirtual());
84 command += ' ' + ProtocolHelper::enabled(d->mCollection.enabled());
85 command += ' ' + ProtocolHelper::listPreference(Collection::ListDisplay, d->mCollection.localListPreference(Collection::ListDisplay));
86 command += ' ' + ProtocolHelper::listPreference(Collection::ListSync, d->mCollection.localListPreference(Collection::ListSync));
87 command += ' ' + ProtocolHelper::listPreference(Collection::ListIndex, d->mCollection.localListPreference(Collection::ListIndex));
88 foreach (Attribute *attr, d->mCollection.attributes()) {
89 command += ' ' + attr->type() + ' ' + ImapParser::quote(attr->serialized());
90 }
91 command += ' ' + ProtocolHelper::cachePolicyToByteArray(d->mCollection.cachePolicy());
92 command += ")\n";
93 d->writeData(command);
94 emitWriteFinished();
95}
96
97Collection CollectionCreateJob::collection() const
98{
99 Q_D(const CollectionCreateJob);
100
101 return d->mCollection;
102}
103
104void CollectionCreateJob::doHandleResponse(const QByteArray &tag, const QByteArray &data)
105{
106 Q_D(CollectionCreateJob);
107
108 if (tag == "*") {
109 Collection col;
110 ProtocolHelper::parseCollection(data, col);
111 if (!col.isValid()) {
112 return;
113 }
114
115 col.setParentCollection(d->mCollection.parentCollection());
116 col.setName(d->mCollection.name());
117 col.setRemoteId(d->mCollection.remoteId());
118 col.setRemoteRevision(d->mCollection.remoteRevision());
119 col.setVirtual(d->mCollection.isVirtual());
120 d->mCollection = col;
121 } else {
122 Job::doHandleResponse(tag, data);
123 }
124}
125