-rw-r--r-- | kabc/vcard/ContentLine.cpp | 289 |
1 files changed, 289 insertions, 0 deletions
diff --git a/kabc/vcard/ContentLine.cpp b/kabc/vcard/ContentLine.cpp new file mode 100644 index 0000000..6fa1a8f --- a/dev/null +++ b/kabc/vcard/ContentLine.cpp | |||
@@ -0,0 +1,289 @@ | |||
1 | /* | ||
2 | libvcard - vCard parsing library for vCard version 3.0 | ||
3 | |||
4 | Copyright (C) 1999 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 | #include <qregexp.h> | ||
27 | |||
28 | #include <kdebug.h> | ||
29 | |||
30 | #include <VCardAdrParam.h> | ||
31 | #include <VCardAgentParam.h> | ||
32 | #include <VCardDateParam.h> | ||
33 | #include <VCardEmailParam.h> | ||
34 | #include <VCardImageParam.h> | ||
35 | #include <VCardSourceParam.h> | ||
36 | #include <VCardTelParam.h> | ||
37 | #include <VCardTextBinParam.h> | ||
38 | #include <VCardTextParam.h> | ||
39 | |||
40 | #include <VCardAdrValue.h> | ||
41 | #include <VCardAgentValue.h> | ||
42 | #include <VCardDateValue.h> | ||
43 | #include <VCardImageValue.h> | ||
44 | #include <VCardTextValue.h> | ||
45 | #include <VCardTextBinValue.h> | ||
46 | #include <VCardLangValue.h> | ||
47 | #include <VCardNValue.h> | ||
48 | #include <VCardURIValue.h> | ||
49 | #include <VCardSoundValue.h> | ||
50 | #include <VCardClassValue.h> | ||
51 | #include <VCardFloatValue.h> | ||
52 | #include <VCardOrgValue.h> | ||
53 | #include <VCardTelValue.h> | ||
54 | #include <VCardTextListValue.h> | ||
55 | #include <VCardUTCValue.h> | ||
56 | #include <VCardGeoValue.h> | ||
57 | |||
58 | #include <VCardRToken.h> | ||
59 | #include <VCardContentLine.h> | ||
60 | |||
61 | #include <VCardEntity.h> | ||
62 | #include <VCardEnum.h> | ||
63 | #include <VCardDefines.h> | ||
64 | |||
65 | using namespace VCARD; | ||
66 | |||
67 | ContentLine::ContentLine() | ||
68 | :Entity(), | ||
69 | value_(0) | ||
70 | { | ||
71 | } | ||
72 | |||
73 | ContentLine::ContentLine(const ContentLine & x) | ||
74 | :Entity(x), | ||
75 | group_ (x.group_), | ||
76 | name_ (x.name_), | ||
77 | paramList_(x.paramList_), | ||
78 | value_(x.value_->clone()) | ||
79 | { | ||
80 | } | ||
81 | |||
82 | ContentLine::ContentLine(const QCString & s) | ||
83 | :Entity(s), | ||
84 | value_(0) | ||
85 | { | ||
86 | } | ||
87 | |||
88 | ContentLine & | ||
89 | ContentLine::operator = (ContentLine & x) | ||
90 | { | ||
91 | if (*this == x) return *this; | ||
92 | |||
93 | paramList_ = x.paramList(); | ||
94 | value_ = x.value_->clone(); | ||
95 | |||
96 | Entity::operator = (x); | ||
97 | return *this; | ||
98 | } | ||
99 | |||
100 | ContentLine & | ||
101 | ContentLine::operator = (const QCString & s) | ||
102 | { | ||
103 | Entity::operator = (s); | ||
104 | delete value_; | ||
105 | value_ = 0; | ||
106 | return *this; | ||
107 | } | ||
108 | |||
109 | bool | ||
110 | ContentLine::operator == (ContentLine & x) | ||
111 | { | ||
112 | x.parse(); | ||
113 | |||
114 | QPtrListIterator<Param> it(x.paramList()); | ||
115 | |||
116 | if (!paramList_.find(it.current())) | ||
117 | return false; | ||
118 | |||
119 | return true; | ||
120 | } | ||
121 | |||
122 | ContentLine::~ContentLine() | ||
123 | { | ||
124 | delete value_; | ||
125 | value_ = 0; | ||
126 | } | ||
127 | |||
128 | void | ||
129 | ContentLine::_parse() | ||
130 | { | ||
131 | vDebug("parse"); | ||
132 | |||
133 | // Unqote newlines | ||
134 | strRep_ = strRep_.replace( QRegExp( "\\\\n" ), "\n" ); | ||
135 | |||
136 | int split = strRep_.find(':'); | ||
137 | |||
138 | if (split == -1) { // invalid content line | ||
139 | vDebug("No ':'"); | ||
140 | return; | ||
141 | } | ||
142 | |||
143 | QCString firstPart(strRep_.left(split)); | ||
144 | QCString valuePart(strRep_.mid(split + 1)); | ||
145 | |||
146 | split = firstPart.find('.'); | ||
147 | |||
148 | if (split != -1) { | ||
149 | group_ = firstPart.left(split); | ||
150 | firstPart= firstPart.mid(split + 1); | ||
151 | } | ||
152 | |||
153 | vDebug("Group == " + group_); | ||
154 | vDebug("firstPart == " + firstPart); | ||
155 | vDebug("valuePart == " + valuePart); | ||
156 | |||
157 | // Now we have the group, the name and param list together and the value. | ||
158 | |||
159 | QStrList l; | ||
160 | |||
161 | RTokenise(firstPart, ";", l); | ||
162 | |||
163 | if (l.count() == 0) {// invalid - no name ! | ||
164 | vDebug("No name for this content line !"); | ||
165 | return; | ||
166 | } | ||
167 | |||
168 | name_ = l.at(0); | ||
169 | |||
170 | // Now we have the name, so the rest of 'l' is the params. | ||
171 | // Remove the name part. | ||
172 | l.remove(0u); | ||
173 | |||
174 | entityType_= EntityNameToEntityType(name_); | ||
175 | paramType_= EntityTypeToParamType(entityType_); | ||
176 | |||
177 | unsigned int i = 0; | ||
178 | |||
179 | // For each parameter, create a new parameter of the correct type. | ||
180 | |||
181 | QStrListIterator it(l); | ||
182 | |||
183 | for (; it.current(); ++it, i++) { | ||
184 | |||
185 | QCString str = *it; | ||
186 | |||
187 | split = str.find("="); | ||
188 | if (split < 0 ) { | ||
189 | vDebug("No '=' in paramter."); | ||
190 | continue; | ||
191 | } | ||
192 | |||
193 | QCString paraName = str.left(split); | ||
194 | QCString paraValue = str.mid(split + 1); | ||
195 | |||
196 | QStrList paraValues; | ||
197 | RTokenise(paraValue, ",", paraValues); | ||
198 | |||
199 | QStrListIterator it2( paraValues ); | ||
200 | |||
201 | for(; it2.current(); ++it2) { | ||
202 | |||
203 | Param *p = new Param; | ||
204 | p->setName( paraName ); | ||
205 | p->setValue( *it2 ); | ||
206 | |||
207 | paramList_.append(p); | ||
208 | } | ||
209 | } | ||
210 | |||
211 | // Create a new value of the correct type. | ||
212 | |||
213 | valueType_ = EntityTypeToValueType(entityType_); | ||
214 | |||
215 | //kdDebug(5710) << "valueType: " << valueType_ << endl; | ||
216 | |||
217 | switch (valueType_) { | ||
218 | |||
219 | case ValueSound: value_ = new SoundValue;break; | ||
220 | case ValueAgent: value_ = new AgentValue;break; | ||
221 | case ValueAddress: value_ = new AdrValue; break; | ||
222 | case ValueTel: value_ = new TelValue; break; | ||
223 | case ValueTextBin: value_ = new TextBinValue;break; | ||
224 | case ValueOrg: value_ = new OrgValue; break; | ||
225 | case ValueN: value_ = new NValue; break; | ||
226 | case ValueUTC: value_ = new UTCValue; break; | ||
227 | case ValueURI: value_ = new URIValue; break; | ||
228 | case ValueClass: value_ = new ClassValue;break; | ||
229 | case ValueFloat: value_ = new FloatValue;break; | ||
230 | case ValueImage: value_ = new ImageValue;break; | ||
231 | case ValueDate: value_ = new DateValue; break; | ||
232 | case ValueTextList: value_ = new TextListValue;break; | ||
233 | case ValueGeo: value_ = new GeoValue; break; | ||
234 | case ValueText: | ||
235 | case ValueUnknown: | ||
236 | default: value_ = new TextValue; break; | ||
237 | } | ||
238 | |||
239 | *value_ = valuePart; | ||
240 | } | ||
241 | |||
242 | void | ||
243 | ContentLine::_assemble() | ||
244 | { | ||
245 | vDebug("Assemble (argl) - my name is \"" + name_ + "\""); | ||
246 | strRep_.truncate(0); | ||
247 | |||
248 | QCString line; | ||
249 | |||
250 | if (!group_.isEmpty()) | ||
251 | line += group_ + '.'; | ||
252 | |||
253 | line += name_; | ||
254 | |||
255 | vDebug("Adding parameters"); | ||
256 | ParamListIterator it(paramList_); | ||
257 | |||
258 | for (; it.current(); ++it) | ||
259 | line += ";" + it.current()->asString(); | ||
260 | |||
261 | vDebug("Adding value"); | ||
262 | if (value_ != 0) | ||
263 | line += ":" + value_->asString(); | ||
264 | else | ||
265 | vDebug("No value"); | ||
266 | |||
267 | // Quote newlines | ||
268 | line = line.replace( QRegExp( "\n" ), "\\n" ); | ||
269 | |||
270 | // Fold lines longer than 72 chars | ||
271 | const int maxLen = 72; | ||
272 | uint cursor = 0; | ||
273 | while( line.length() > ( cursor + 1 ) * maxLen ) { | ||
274 | strRep_ += line.mid( cursor * maxLen, maxLen ); | ||
275 | strRep_ += "\r\n "; | ||
276 | ++cursor; | ||
277 | } | ||
278 | strRep_ += line.mid( cursor * maxLen ); | ||
279 | } | ||
280 | |||
281 | void | ||
282 | ContentLine::clear() | ||
283 | { | ||
284 | group_.truncate(0); | ||
285 | name_.truncate(0); | ||
286 | paramList_.clear(); | ||
287 | delete value_; | ||
288 | value_ = 0; | ||
289 | } | ||