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 "tagcreatejob.h" |
21 | #include "job_p.h" |
22 | #include "tag.h" |
23 | #include "protocolhelper_p.h" |
24 | #include <KLocalizedString> |
25 | |
26 | using namespace Akonadi; |
27 | |
28 | struct Akonadi::TagCreateJobPrivate : public JobPrivate |
29 | { |
30 | TagCreateJobPrivate(TagCreateJob *parent) |
31 | : JobPrivate(parent) |
32 | , mMerge(false) |
33 | { |
34 | } |
35 | |
36 | Tag mTag; |
37 | Tag mResultTag; |
38 | bool mMerge; |
39 | }; |
40 | |
41 | TagCreateJob::TagCreateJob(const Akonadi::Tag &tag, QObject *parent) |
42 | : Job(new TagCreateJobPrivate(this), parent) |
43 | { |
44 | Q_D(TagCreateJob); |
45 | d->mTag = tag; |
46 | } |
47 | |
48 | void TagCreateJob::setMergeIfExisting(bool merge) |
49 | { |
50 | Q_D(TagCreateJob); |
51 | d->mMerge = merge; |
52 | } |
53 | |
54 | void TagCreateJob::doStart() |
55 | { |
56 | Q_D(TagCreateJob); |
57 | |
58 | if (d->mTag.gid().isEmpty()) { |
59 | kWarning() << "The gid of a new tag must not be empty" ; |
60 | setError(Job::Unknown); |
61 | setErrorText(i18n("Failed to create tag." )); |
62 | emitResult(); |
63 | return; |
64 | } |
65 | |
66 | QByteArray command = d->newTag() + " TAGAPPEND (" ; |
67 | |
68 | QList<QByteArray> list; |
69 | list << "GID" ; |
70 | list << ImapParser::quote(d->mTag.gid()); |
71 | |
72 | if (d->mMerge) { |
73 | list << "MERGE" ; |
74 | } |
75 | |
76 | if (!d->mTag.type().isEmpty()) { |
77 | list << "MIMETYPE" ; |
78 | list << ImapParser::quote(d->mTag.type()); |
79 | } |
80 | if (!d->mTag.remoteId().isEmpty()) { |
81 | list << "REMOTEID" ; |
82 | list << ImapParser::quote(d->mTag.remoteId()); |
83 | } |
84 | if (d->mTag.parent().isValid()) { |
85 | list << "PARENT" ; |
86 | list << QString::number(d->mTag.parent().id()).toLatin1(); |
87 | } |
88 | command += ImapParser::join(list, " " ); |
89 | command += " " ; // list of parts |
90 | const QByteArray attrs = ProtocolHelper::attributesToByteArray(d->mTag, false); |
91 | if (!attrs.isEmpty()) { |
92 | command += attrs; |
93 | } |
94 | command += ")" ; |
95 | |
96 | d->writeData(command); |
97 | } |
98 | |
99 | void TagCreateJob::doHandleResponse(const QByteArray &tag, const QByteArray &data) |
100 | { |
101 | Q_D(TagCreateJob); |
102 | |
103 | if (tag == "*" ) { |
104 | int begin = data.indexOf("TAGFETCH" ); |
105 | if (begin >= 0) { |
106 | // split fetch response into key/value pairs |
107 | QList<QByteArray> fetchResponse; |
108 | ImapParser::parseParenthesizedList(data, fetchResponse, begin + 8); |
109 | if (!d->mMerge) { |
110 | //If merge is enabled there is the possibility that existing attributes etc are not valid anymore |
111 | d->mResultTag = d->mTag; |
112 | } |
113 | ProtocolHelper::parseTagFetchResult(fetchResponse, d->mResultTag); |
114 | } |
115 | } |
116 | } |
117 | |
118 | Tag TagCreateJob::tag() const |
119 | { |
120 | Q_D(const TagCreateJob); |
121 | return d->mResultTag; |
122 | } |
123 | |