summaryrefslogtreecommitdiffabout
path: root/kabc/vcard/VCardv.cpp
Unidiff
Diffstat (limited to 'kabc/vcard/VCardv.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--kabc/vcard/VCardv.cpp282
1 files changed, 282 insertions, 0 deletions
diff --git a/kabc/vcard/VCardv.cpp b/kabc/vcard/VCardv.cpp
new file mode 100644
index 0000000..8d271f4
--- a/dev/null
+++ b/kabc/vcard/VCardv.cpp
@@ -0,0 +1,282 @@
1/*
2 libvcard - vCard parsing library for vCard version 3.0
3
4 Copyright (C) 1998 Rik Hemsley rik@kde.org
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to
8 deal in the Software without restriction, including without limitation the
9 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 sell copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22*/
23
24#include <qcstring.h>
25#include <qstrlist.h>
26
27#include <VCardEntity.h>
28#include <VCardVCard.h>
29#include <VCardContentLine.h>
30#include <VCardRToken.h>
31
32#include <VCardDefines.h>
33
34using namespace VCARD;
35
36VCard::VCard()
37 :Entity()
38{
39}
40
41VCard::VCard(const VCard & x)
42 :Entity(x),
43 group_(x.group_),
44 contentLineList_(x.contentLineList_)
45{
46}
47
48VCard::VCard(const QCString & s)
49 :Entity(s)
50{
51}
52
53 VCard &
54VCard::operator = (VCard & x)
55{
56 if (*this == x) return *this;
57
58 group_ = x.group();
59 contentLineList_= x.contentLineList_;
60
61 Entity::operator = (x);
62 return *this;
63}
64
65 VCard &
66VCard::operator = (const QCString & s)
67{
68 Entity::operator = (s);
69 return *this;
70}
71
72 bool
73VCard::operator == (VCard & x)
74{
75 x.parse();
76 return false;
77}
78
79VCard::~VCard()
80{
81}
82
83 void
84VCard::_parse()
85{
86 vDebug("parse() called");
87 QStrList l;
88
89 RTokenise(strRep_, "\r\n", l);
90
91 if (l.count() < 3) { // Invalid VCARD !
92 vDebug("Invalid vcard");
93 return;
94 }
95
96 // Get the first line
97 QCString beginLine = QCString(l.at(0)).stripWhiteSpace();
98
99 vDebug("Begin line == \"" + beginLine + "\"");
100
101 // Remove extra blank lines
102 while (QCString(l.last()).isEmpty())
103 l.remove(l.last());
104
105 // Now we know this is the last line
106 QCString endLine = l.last();
107
108 // Trash the first and last lines as we have seen them.
109 l.remove(0u);
110 l.remove(l.last());
111
112 ///////////////////////////////////////////////////////////////
113 // FIRST LINE
114
115 int split = beginLine.find(':');
116
117 if (split == -1) { // invalid, no BEGIN
118 vDebug("No split");
119 return;
120 }
121
122 QCString firstPart(beginLine.left(split));
123 QCString valuePart(beginLine.mid(split + 1));
124
125 split = firstPart.find('.');
126
127 if (split != -1) {
128 group_ = firstPart.left(split);
129 firstPart= firstPart.right(firstPart.length() - split - 1);
130 }
131
132 if (qstrnicmp(firstPart, "BEGIN", 5) != 0) { // No BEGIN !
133 vDebug("No BEGIN");
134 return;
135 }
136
137 if (qstrnicmp(valuePart, "VCARD", 5) != 0) { // Not a vcard !
138 vDebug("No VCARD");
139 return;
140 }
141
142 ///////////////////////////////////////////////////////////////
143 // CONTENT LINES
144 //
145 vDebug("Content lines");
146
147 // Handle folded lines.
148
149 QStrList refolded;
150
151 QStrListIterator it(l);
152
153 QCString cur;
154
155 for (; it.current(); ++it) {
156
157 cur = it.current();
158
159 ++it;
160
161 while (
162 it.current() &&
163 it.current()[0] == ' '&&
164 strlen(it.current()) != 1)
165 {
166 cur += it.current() + 1;
167 ++it;
168 }
169
170 --it;
171
172 refolded.append(cur);
173 }
174
175 QStrListIterator it2(refolded);
176
177 for (; it2.current(); ++it2) {
178
179 vDebug("New contentline using \"" + QCString(it2.current()) + "\"");
180 ContentLine * cl = new ContentLine(it2.current());
181
182 cl->parse();
183
184 contentLineList_.append(cl);
185 }
186
187 ///////////////////////////////////////////////////////////////
188 // LAST LINE
189
190 split = endLine.find(':');
191
192 if (split == -1) // invalid, no END
193 return;
194
195 firstPart = endLine.left(split);
196 valuePart = endLine.right(firstPart.length() - split - 1);
197
198 split = firstPart.find('.');
199
200 if (split != -1) {
201 group_ = firstPart.left(split);
202 firstPart= firstPart.right(firstPart.length() - split - 1);
203 }
204
205 if (qstricmp(firstPart, "END") != 0) // No END !
206 return;
207
208 if (qstricmp(valuePart, "VCARD") != 0) // Not a vcard !
209 return;
210}
211
212 void
213VCard::_assemble()
214{
215 vDebug("Assembling vcard");
216 strRep_ = "BEGIN:VCARD\r\n";
217 strRep_ += "VERSION:3.0\r\n";
218
219 QPtrListIterator<ContentLine> it(contentLineList_);
220
221 for (; it.current(); ++it)
222 strRep_ += it.current()->asString() + "\r\n";
223
224 strRep_ += "END:VCARD\r\n";
225}
226
227 bool
228VCard::has(EntityType t)
229{
230 parse();
231 return contentLine(t) == 0 ? false : true;
232}
233
234 bool
235VCard::has(const QCString & s)
236{
237 parse();
238 return contentLine(s) == 0 ? false : true;
239}
240
241 void
242VCard::add(const ContentLine & cl)
243{
244 parse();
245 ContentLine * c = new ContentLine(cl);
246 contentLineList_.append(c);
247}
248
249 void
250VCard::add(const QCString & s)
251{
252 parse();
253 ContentLine * c = new ContentLine(s);
254 contentLineList_.append(c);
255}
256
257 ContentLine *
258VCard::contentLine(EntityType t)
259{
260 parse();
261 QPtrListIterator<ContentLine> it(contentLineList_);
262
263 for (; it.current(); ++it)
264 if (it.current()->entityType() == t)
265 return it.current();
266
267 return 0;
268}
269
270 ContentLine *
271VCard::contentLine(const QCString & s)
272{
273 parse();
274 QPtrListIterator<ContentLine> it(contentLineList_);
275
276 for (; it.current(); ++it)
277 if (it.current()->entityType() == EntityNameToEntityType(s))
278 return it.current();
279
280 return 0;
281}
282