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 "tag.h" |
21 | #include <akonadi/tagattribute.h> |
22 | |
23 | using namespace Akonadi; |
24 | |
25 | const char *Akonadi::Tag::PLAIN = "PLAIN" ; |
26 | |
27 | struct Akonadi::Tag::Private { |
28 | Private() |
29 | : id(-1) |
30 | {} |
31 | |
32 | ~Private() |
33 | {} |
34 | |
35 | Id id; |
36 | QByteArray gid; |
37 | QByteArray remoteId; |
38 | QScopedPointer<Tag> parent; |
39 | QByteArray type; |
40 | }; |
41 | |
42 | Tag::Tag() |
43 | : AttributeEntity() |
44 | , d(new Private) |
45 | { |
46 | |
47 | } |
48 | |
49 | Tag::Tag(Tag::Id id) |
50 | : AttributeEntity() |
51 | , d(new Private) |
52 | { |
53 | d->id = id; |
54 | } |
55 | |
56 | Tag::Tag(const QString &name) |
57 | : AttributeEntity() |
58 | , d(new Private) |
59 | { |
60 | d->gid = name.toUtf8(); |
61 | setName(name); |
62 | d->type = PLAIN; |
63 | } |
64 | |
65 | Tag::Tag(const Tag &other) |
66 | : AttributeEntity() |
67 | , d(new Private) |
68 | { |
69 | operator=(other); |
70 | } |
71 | |
72 | Tag::~Tag() |
73 | { |
74 | } |
75 | |
76 | Tag &Tag::operator=(const Tag &other) |
77 | { |
78 | d->id = other.d->id; |
79 | d->gid = other.d->gid; |
80 | d->remoteId = other.d->remoteId; |
81 | d->type = other.d->type; |
82 | if (other.d->parent) { |
83 | d->parent.reset(new Tag(*other.d->parent)); |
84 | } |
85 | AttributeEntity::operator=(other); |
86 | return *this; |
87 | } |
88 | |
89 | AttributeEntity &Tag::operator=(const AttributeEntity &other) |
90 | { |
91 | return operator=(*static_cast<const Tag *>(&other)); |
92 | } |
93 | |
94 | bool Tag::operator==(const Tag &other) const |
95 | { |
96 | if (isValid() && other.isValid()) { |
97 | return d->id == other.d->id; |
98 | } |
99 | return d->gid == other.d->gid; |
100 | } |
101 | |
102 | Tag Tag::fromUrl(const KUrl &url) |
103 | { |
104 | if (url.protocol() != QLatin1String("akonadi" )) { |
105 | return Tag(); |
106 | } |
107 | |
108 | const QString tagStr = url.queryItem(QLatin1String("tag" )); |
109 | bool ok = false; |
110 | Tag::Id itemId = tagStr.toLongLong(&ok); |
111 | if (!ok) { |
112 | return Tag(); |
113 | } |
114 | |
115 | return Tag(itemId); |
116 | } |
117 | |
118 | KUrl Tag::url() const |
119 | { |
120 | KUrl url; |
121 | url.setProtocol(QString::fromLatin1("akonadi" )); |
122 | url.addQueryItem(QLatin1String("tag" ), QString::number(id())); |
123 | return url; |
124 | } |
125 | |
126 | void Tag::setId(Tag::Id identifier) |
127 | { |
128 | d->id = identifier; |
129 | } |
130 | |
131 | Tag::Id Tag::id() const |
132 | { |
133 | return d->id; |
134 | } |
135 | |
136 | void Tag::setGid(const QByteArray &gid) const |
137 | { |
138 | d->gid = gid; |
139 | } |
140 | |
141 | QByteArray Tag::gid() const |
142 | { |
143 | return d->gid; |
144 | } |
145 | |
146 | void Tag::setRemoteId(const QByteArray &remoteId) const |
147 | { |
148 | d->remoteId = remoteId; |
149 | } |
150 | |
151 | QByteArray Tag::remoteId() const |
152 | { |
153 | return d->remoteId; |
154 | } |
155 | |
156 | void Tag::setName(const QString &name) |
157 | { |
158 | if (!name.isEmpty()) { |
159 | TagAttribute *const attr = attribute<TagAttribute>(AttributeEntity::AddIfMissing); |
160 | attr->setDisplayName(name); |
161 | } |
162 | } |
163 | |
164 | QString Tag::name() const |
165 | { |
166 | const TagAttribute *const attr = attribute<TagAttribute>(); |
167 | const QString displayName = attr ? attr->displayName() : QString(); |
168 | return !displayName.isEmpty() ? displayName : QString::fromUtf8(d->gid); |
169 | } |
170 | |
171 | void Tag::setParent(const Tag &parent) |
172 | { |
173 | if (parent.isValid()) { |
174 | d->parent.reset(new Tag(parent)); |
175 | } |
176 | } |
177 | |
178 | Tag Tag::parent() const |
179 | { |
180 | if (!d->parent) { |
181 | return Tag(); |
182 | } |
183 | return *d->parent; |
184 | } |
185 | |
186 | void Tag::setType(const QByteArray &type) const |
187 | { |
188 | d->type = type; |
189 | } |
190 | |
191 | QByteArray Tag::type() const |
192 | { |
193 | return d->type; |
194 | } |
195 | |
196 | bool Tag::isValid() const |
197 | { |
198 | return d->id >= 0; |
199 | } |
200 | |
201 | bool Tag::isImmutable() const |
202 | { |
203 | return (d->type.isEmpty() || d->type == PLAIN); |
204 | } |
205 | |
206 | uint qHash(const Tag &tag) |
207 | { |
208 | return qHash(tag.id()); |
209 | } |
210 | |
211 | QDebug &operator<<(QDebug &debug, const Tag &tag) |
212 | { |
213 | debug << "Akonadi::Tag( ID " << tag.id() << ", GID " << tag.gid() << ", parent" << tag.parent().id() << ")" ; |
214 | return debug; |
215 | } |
216 | |