1 | /* |
2 | Copyright (c) 2014 Christian Mollekopf <mollekopf@kolabsys.com> |
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 "tagmodifyjob.h" |
21 | #include "job_p.h" |
22 | #include "tag.h" |
23 | #include "protocolhelper_p.h" |
24 | #include "changemediator_p.h" |
25 | |
26 | using namespace Akonadi; |
27 | |
28 | struct Akonadi::TagModifyJobPrivate : public JobPrivate |
29 | { |
30 | TagModifyJobPrivate(TagModifyJob *parent) |
31 | : JobPrivate(parent) |
32 | { |
33 | } |
34 | |
35 | Tag mTag; |
36 | }; |
37 | |
38 | TagModifyJob::TagModifyJob(const Akonadi::Tag &tag, QObject *parent) |
39 | : Job(new TagModifyJobPrivate(this), parent) |
40 | { |
41 | Q_D(TagModifyJob); |
42 | d->mTag = tag; |
43 | } |
44 | |
45 | void TagModifyJob::doStart() |
46 | { |
47 | Q_D(TagModifyJob); |
48 | |
49 | QList<QByteArray> list; |
50 | if (!d->mTag.remoteId().isEmpty()) { |
51 | list << "REMOTEID" ; |
52 | list << ImapParser::quote(d->mTag.remoteId()); |
53 | } |
54 | if (!d->mTag.type().isEmpty()) { |
55 | list << "MIMETYPE" ; |
56 | list << ImapParser::quote(d->mTag.type()); |
57 | } |
58 | if (d->mTag.parent().isValid() && !d->mTag.isImmutable()) { |
59 | list << "PARENT" ; |
60 | list << QString::number(d->mTag.parent().id()).toLatin1(); |
61 | } |
62 | |
63 | QByteArray command = d->newTag(); |
64 | try { |
65 | command += ProtocolHelper::tagSetToByteArray(Tag::List() << d->mTag, "TAGSTORE" ); |
66 | } catch (const std::exception &e) { |
67 | setError(Unknown); |
68 | setErrorText(QString::fromUtf8(e.what())); |
69 | emitResult(); |
70 | return; |
71 | } |
72 | command += " (" ; |
73 | command += ImapParser::join(list, " " ); |
74 | command += " " ; |
75 | |
76 | if (!d->mTag.attributes().isEmpty()) { |
77 | command += ProtocolHelper::attributesToByteArray(d->mTag, false); |
78 | } |
79 | if (!d->mTag.removedAttributes().isEmpty()) { |
80 | QList<QByteArray> l; |
81 | Q_FOREACH (const QByteArray &attr, d->mTag.removedAttributes()) { |
82 | l << '-' + attr; |
83 | } |
84 | command += ImapParser::join(l, " " ); |
85 | } |
86 | |
87 | command += ")" ; |
88 | |
89 | d->writeData(command); |
90 | } |
91 | |
92 | void TagModifyJob::doHandleResponse(const QByteArray &tag, const QByteArray &data) |
93 | { |
94 | Q_D(TagModifyJob); |
95 | Q_UNUSED(tag); |
96 | |
97 | if (data.startsWith("OK" )) { //krazy:exclude=strings |
98 | ChangeMediator::invalidateTag(d->mTag); |
99 | emitResult(); |
100 | } |
101 | } |
102 | |