1 | /* |
2 | Copyright (c) 2011 Christian Mollekopf <chrigi_1@fastmail.fm> |
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 "entitydeletedattribute.h" |
21 | |
22 | #include "imapparser_p.h" |
23 | |
24 | #include <QtCore/QByteArray> |
25 | #include <QtCore/QString> |
26 | |
27 | using namespace Akonadi; |
28 | |
29 | class EntityDeletedAttribute::EntityDeletedAttributePrivate |
30 | { |
31 | public: |
32 | EntityDeletedAttributePrivate() {}; |
33 | |
34 | Collection restoreCollection; |
35 | QString restoreResource; |
36 | }; |
37 | |
38 | EntityDeletedAttribute::EntityDeletedAttribute() |
39 | : d_ptr(new EntityDeletedAttributePrivate()) |
40 | { |
41 | |
42 | } |
43 | |
44 | EntityDeletedAttribute::~EntityDeletedAttribute() |
45 | { |
46 | delete d_ptr; |
47 | } |
48 | |
49 | void EntityDeletedAttribute::setRestoreCollection(const Akonadi::Collection &collection) |
50 | { |
51 | Q_D(EntityDeletedAttribute); |
52 | if (!collection.isValid()) { |
53 | kWarning() << "invalid collection" << collection; |
54 | } |
55 | Q_ASSERT(collection.isValid()); |
56 | d->restoreCollection = collection; |
57 | if (collection.resource().isEmpty()) { |
58 | kWarning() << "no resource set" ; |
59 | } |
60 | d->restoreResource = collection.resource(); |
61 | } |
62 | |
63 | Collection EntityDeletedAttribute::restoreCollection() const |
64 | { |
65 | Q_D(const EntityDeletedAttribute); |
66 | return d->restoreCollection; |
67 | } |
68 | |
69 | QString EntityDeletedAttribute::restoreResource() const |
70 | { |
71 | Q_D(const EntityDeletedAttribute); |
72 | return d->restoreResource; |
73 | } |
74 | |
75 | QByteArray Akonadi::EntityDeletedAttribute::type() const |
76 | { |
77 | return "DELETED" ; |
78 | } |
79 | |
80 | EntityDeletedAttribute *EntityDeletedAttribute::clone() const |
81 | { |
82 | const Q_D(EntityDeletedAttribute); |
83 | EntityDeletedAttribute *attr = new EntityDeletedAttribute(); |
84 | attr->d_ptr->restoreCollection = d->restoreCollection; |
85 | attr->d_ptr->restoreResource = d->restoreResource; |
86 | return attr; |
87 | } |
88 | |
89 | QByteArray EntityDeletedAttribute::serialized() const |
90 | { |
91 | const Q_D(EntityDeletedAttribute); |
92 | |
93 | QList<QByteArray> l; |
94 | l << ImapParser::quote(d->restoreResource.toUtf8()); |
95 | QList<QByteArray> components; |
96 | components << QByteArray::number(d->restoreCollection.id()); |
97 | |
98 | l << '(' + ImapParser::join(components, " " ) + ')'; |
99 | return '(' + ImapParser::join(l, " " ) + ')'; |
100 | } |
101 | |
102 | void EntityDeletedAttribute::deserialize(const QByteArray &data) |
103 | { |
104 | Q_D(EntityDeletedAttribute); |
105 | |
106 | QList<QByteArray> l; |
107 | ImapParser::parseParenthesizedList(data, l); |
108 | if (l.size() != 2) { |
109 | kWarning() << "invalid size" ; |
110 | return; |
111 | } |
112 | d->restoreResource = QString::fromUtf8(l[0]); |
113 | |
114 | if (!l[1].isEmpty()) { |
115 | QList<QByteArray> componentData; |
116 | ImapParser::parseParenthesizedList(l[1], componentData); |
117 | if (componentData.size() != 1) { |
118 | return; |
119 | } |
120 | QList<int> components; |
121 | bool ok; |
122 | for (int i = 0; i < 1; ++i) { |
123 | components << componentData.at(i).toInt(&ok); |
124 | if (!ok) { |
125 | return; |
126 | } |
127 | } |
128 | d->restoreCollection = Collection(components.at(0)); |
129 | } |
130 | } |
131 | |