summaryrefslogtreecommitdiffabout
path: root/kabc/vcard21parser.cpp
Unidiff
Diffstat (limited to 'kabc/vcard21parser.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--kabc/vcard21parser.cpp14
1 files changed, 6 insertions, 8 deletions
diff --git a/kabc/vcard21parser.cpp b/kabc/vcard21parser.cpp
index 277de22..60d02b8 100644
--- a/kabc/vcard21parser.cpp
+++ b/kabc/vcard21parser.cpp
@@ -1,609 +1,607 @@
1/* 1/*
2 This file is part of libkabc. 2 This file is part of libkabc.
3 Copyright (c) 2001 Mark Westcott <mark@houseoffish.org> 3 Copyright (c) 2001 Mark Westcott <mark@houseoffish.org>
4 4
5 This library is free software; you can redistribute it and/or 5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public 6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either 7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version. 8 version 2 of the License, or (at your option) any later version.
9 9
10 This library is distributed in the hope that it will be useful, 10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details. 13 Library General Public License for more details.
14 14
15 You should have received a copy of the GNU Library General Public License 15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to 16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. 18 Boston, MA 02111-1307, USA.
19*/ 19*/
20 20
21/* 21/*
22Enhanced Version of the file for platform independent KDE tools. 22Enhanced Version of the file for platform independent KDE tools.
23Copyright (c) 2004 Ulf Schenk 23Copyright (c) 2004 Ulf Schenk
24 24
25$Id$ 25$Id$
26*/ 26*/
27 27
28#include <qmap.h> 28#include <qmap.h>
29#include <qregexp.h> 29#include <qregexp.h>
30#include <kmdcodec.h> 30#include <kmdcodec.h>
31 31
32#include "vcard21parser.h" 32#include "vcard21parser.h"
33#include "vcardconverter.h" 33#include "vcardconverter.h"
34 34
35using namespace KABC; 35using namespace KABC;
36 36
37bool VCardLineX::isValid() const 37bool VCardLineX::isValid() const
38{ 38{
39 // Invalid: if it is "begin:vcard" or "end:vcard" 39 // Invalid: if it is "begin:vcard" or "end:vcard"
40 if ( name == VCARD_BEGIN_N || name == VCARD_END_N ) 40 if ( name == VCARD_BEGIN_N || name == VCARD_END_N )
41 return false; 41 return false;
42 42
43 if ( name[0] == 'x' && name[1] == '-' ) // A custom x- line 43 if ( name[0] == 'x' && name[1] == '-' ) // A custom x- line
44 return true; 44 return true;
45 45
46 // This is long but it makes it a bit faster (and saves me from using 46 // This is long but it makes it a bit faster (and saves me from using
47 // a tree which is probably the ideal situation, but a bit memory heavy) 47 // a tree which is probably the ideal situation, but a bit memory heavy)
48 switch( name[0] ) { 48 switch( name[0] ) {
49 case 'a': 49 case 'a':
50 if ( name == VCARD_ADR && qualified && 50 if ( name == VCARD_ADR && qualified &&
51 (qualifiers.contains(VCARD_ADR_DOM) || 51 (qualifiers.contains(VCARD_ADR_DOM) ||
52 qualifiers.contains(VCARD_ADR_INTL) || 52 qualifiers.contains(VCARD_ADR_INTL) ||
53 qualifiers.contains(VCARD_ADR_POSTAL) || 53 qualifiers.contains(VCARD_ADR_POSTAL) ||
54 qualifiers.contains(VCARD_ADR_HOME) || 54 qualifiers.contains(VCARD_ADR_HOME) ||
55 qualifiers.contains(VCARD_ADR_WORK) || 55 qualifiers.contains(VCARD_ADR_WORK) ||
56 qualifiers.contains(VCARD_ADR_PREF) 56 qualifiers.contains(VCARD_ADR_PREF)
57 ) ) 57 ) )
58 return true; 58 return true;
59 59
60 if ( name == VCARD_AGENT ) 60 if ( name == VCARD_AGENT )
61 return true; 61 return true;
62 break; 62 break;
63 63
64 case 'b': 64 case 'b':
65 if ( name == VCARD_BDAY ) 65 if ( name == VCARD_BDAY )
66 return true; 66 return true;
67 break; 67 break;
68 68
69 case 'c': 69 case 'c':
70 if ( name == VCARD_CATEGORIES ) 70 if ( name == VCARD_CATEGORIES )
71 return true; 71 return true;
72 if ( name == VCARD_CLASS && qualified && 72 if ( name == VCARD_CLASS && qualified &&
73 (qualifiers.contains(VCARD_CLASS_PUBLIC) || 73 (qualifiers.contains(VCARD_CLASS_PUBLIC) ||
74 qualifiers.contains(VCARD_CLASS_PRIVATE) || 74 qualifiers.contains(VCARD_CLASS_PRIVATE) ||
75 qualifiers.contains(VCARD_CLASS_CONFIDENTIAL) 75 qualifiers.contains(VCARD_CLASS_CONFIDENTIAL)
76 ) ) 76 ) )
77 return true; 77 return true;
78 break; 78 break;
79 79
80 case 'e': 80 case 'e':
81 if ( name == VCARD_EMAIL && qualified && 81 if ( name == VCARD_EMAIL && qualified &&
82 (qualifiers.contains(VCARD_EMAIL_INTERNET) || 82 (qualifiers.contains(VCARD_EMAIL_INTERNET) ||
83 qualifiers.contains(VCARD_EMAIL_PREF) || 83 qualifiers.contains(VCARD_EMAIL_PREF) ||
84 qualifiers.contains(VCARD_EMAIL_X400) 84 qualifiers.contains(VCARD_EMAIL_X400)
85 ) ) 85 ) )
86 return true; 86 return true;
87 break; 87 break;
88 88
89 case 'f': 89 case 'f':
90 if ( name == VCARD_FN ) 90 if ( name == VCARD_FN )
91 return true; 91 return true;
92 break; 92 break;
93 93
94 case 'g': 94 case 'g':
95 if ( name == VCARD_GEO ) 95 if ( name == VCARD_GEO )
96 return true; 96 return true;
97 break; 97 break;
98 98
99 case 'k': 99 case 'k':
100 if ( name == VCARD_KEY && qualified && 100 if ( name == VCARD_KEY && qualified &&
101 (qualifiers.contains(VCARD_KEY_X509) || 101 (qualifiers.contains(VCARD_KEY_X509) ||
102 qualifiers.contains(VCARD_KEY_PGP) 102 qualifiers.contains(VCARD_KEY_PGP)
103 ) ) 103 ) )
104 return true; 104 return true;
105 break; 105 break;
106 106
107 case 'l': 107 case 'l':
108 if ( name == VCARD_LABEL ) 108 if ( name == VCARD_LABEL )
109 return true; 109 return true;
110 if ( name == VCARD_LOGO ) 110 if ( name == VCARD_LOGO )
111 return true; 111 return true;
112 break; 112 break;
113 113
114 case 'm': 114 case 'm':
115 if ( name == VCARD_MAILER ) 115 if ( name == VCARD_MAILER )
116 return true; 116 return true;
117 break; 117 break;
118 118
119 case 'n': 119 case 'n':
120 if ( name == VCARD_N ) 120 if ( name == VCARD_N )
121 return true; 121 return true;
122 if ( name == VCARD_NAME ) 122 if ( name == VCARD_NAME )
123 return true; 123 return true;
124 if ( name == VCARD_NICKNAME ) 124 if ( name == VCARD_NICKNAME )
125 return true; 125 return true;
126 if ( name == VCARD_NOTE ) 126 if ( name == VCARD_NOTE )
127 return true; 127 return true;
128 break; 128 break;
129 129
130 case 'o': 130 case 'o':
131 if ( name == VCARD_ORG ) 131 if ( name == VCARD_ORG )
132 return true; 132 return true;
133 break; 133 break;
134 134
135 case 'p': 135 case 'p':
136 if ( name == VCARD_PHOTO ) 136 if ( name == VCARD_PHOTO )
137 return true; 137 return true;
138 if ( name == VCARD_PROFILE ) 138 if ( name == VCARD_PROFILE )
139 return true; 139 return true;
140 if ( name == VCARD_PRODID ) 140 if ( name == VCARD_PRODID )
141 return true; 141 return true;
142 break; 142 break;
143 143
144 case 'r': 144 case 'r':
145 if ( name == VCARD_ROLE ) 145 if ( name == VCARD_ROLE )
146 return true; 146 return true;
147 if ( name == VCARD_REV ) 147 if ( name == VCARD_REV )
148 return true; 148 return true;
149 break; 149 break;
150 150
151 case 's': 151 case 's':
152 if ( name == VCARD_SOURCE ) 152 if ( name == VCARD_SOURCE )
153 return true; 153 return true;
154 if ( name == VCARD_SOUND ) 154 if ( name == VCARD_SOUND )
155 return true; 155 return true;
156 break; 156 break;
157 157
158 case 't': 158 case 't':
159 if ( name == VCARD_TEL && qualified && 159 if ( name == VCARD_TEL && qualified &&
160 (qualifiers.contains(VCARD_TEL_HOME) || 160 (qualifiers.contains(VCARD_TEL_HOME) ||
161 qualifiers.contains(VCARD_TEL_WORK) || 161 qualifiers.contains(VCARD_TEL_WORK) ||
162 qualifiers.contains(VCARD_TEL_PREF) || 162 qualifiers.contains(VCARD_TEL_PREF) ||
163 qualifiers.contains(VCARD_TEL_VOICE) || 163 qualifiers.contains(VCARD_TEL_VOICE) ||
164 qualifiers.contains(VCARD_TEL_FAX) || 164 qualifiers.contains(VCARD_TEL_FAX) ||
165 qualifiers.contains(VCARD_TEL_MSG) || 165 qualifiers.contains(VCARD_TEL_MSG) ||
166 qualifiers.contains(VCARD_TEL_CELL) || 166 qualifiers.contains(VCARD_TEL_CELL) ||
167 qualifiers.contains(VCARD_TEL_PAGER) || 167 qualifiers.contains(VCARD_TEL_PAGER) ||
168 qualifiers.contains(VCARD_TEL_BBS) || 168 qualifiers.contains(VCARD_TEL_BBS) ||
169 qualifiers.contains(VCARD_TEL_MODEM) || 169 qualifiers.contains(VCARD_TEL_MODEM) ||
170 qualifiers.contains(VCARD_TEL_CAR) || 170 qualifiers.contains(VCARD_TEL_CAR) ||
171 qualifiers.contains(VCARD_TEL_ISDN) || 171 qualifiers.contains(VCARD_TEL_ISDN) ||
172 qualifiers.contains(VCARD_TEL_VIDEO) || 172 qualifiers.contains(VCARD_TEL_VIDEO) ||
173 qualifiers.contains(VCARD_TEL_PCS) || 173 qualifiers.contains(VCARD_TEL_PCS)
174 qualifiers.contains(VCARD_TEL_SIP)
175 ) ) 174 ) )
176 return true; 175 return true;
177 if ( name == VCARD_TZ ) 176 if ( name == VCARD_TZ )
178 return true; 177 return true;
179 if ( name == VCARD_TITLE ) 178 if ( name == VCARD_TITLE )
180 return true; 179 return true;
181 break; 180 break;
182 181
183 case 'u': 182 case 'u':
184 if ( name == VCARD_URL ) 183 if ( name == VCARD_URL )
185 return true; 184 return true;
186 if ( name == VCARD_UID ) 185 if ( name == VCARD_UID )
187 return true; 186 return true;
188 break; 187 break;
189 188
190 case 'v': 189 case 'v':
191 if ( name == VCARD_VERSION ) 190 if ( name == VCARD_VERSION )
192 return true; 191 return true;
193 break; 192 break;
194 default: 193 default:
195 break; 194 break;
196 } 195 }
197 196
198 return false; 197 return false;
199} 198}
200 199
201 200
202VCard21Parser::VCard21Parser() 201VCard21Parser::VCard21Parser()
203{ 202{
204} 203}
205 204
206VCard21Parser::~VCard21Parser() 205VCard21Parser::~VCard21Parser()
207{ 206{
208} 207}
209 208
210void VCard21Parser::readFromString(KABC::AddressBook *addressbook, const QString &data) 209void VCard21Parser::readFromString(KABC::AddressBook *addressbook, const QString &data)
211{ 210{
212 KABC::Addressee mAddressee = readFromString(data); 211 KABC::Addressee mAddressee = readFromString(data);
213 addressbook->insertAddressee(mAddressee); 212 addressbook->insertAddressee(mAddressee);
214} 213}
215 214
216KABC::Addressee VCard21Parser::readFromString( const QString &data) 215KABC::Addressee VCard21Parser::readFromString( const QString &data)
217{ 216{
218 KABC::Addressee addressee; 217 KABC::Addressee addressee;
219 VCard21ParserImpl *mVCard = VCard21ParserImpl::parseVCard(data); 218 VCard21ParserImpl *mVCard = VCard21ParserImpl::parseVCard(data);
220 QString tmpStr; 219 QString tmpStr;
221 220
222 // Check if parsing failed 221 // Check if parsing failed
223 if (mVCard == 0) 222 if (mVCard == 0)
224 { 223 {
225 kdDebug() << "Parsing failed" << endl; 224 kdDebug() << "Parsing failed" << endl;
226 return addressee; 225 return addressee;
227 } 226 }
228 //set the addressees name and formated name 227 //set the addressees name and formated name
229 QStringList tmpList = mVCard->getValues(VCARD_N); 228 QStringList tmpList = mVCard->getValues(VCARD_N);
230 QString formattedName = ""; 229 QString formattedName = "";
231 if (tmpList.count() > 0) 230 if (tmpList.count() > 0)
232 addressee.setFamilyName(tmpList[0]); 231 addressee.setFamilyName(tmpList[0]);
233 if (tmpList.count() > 1) 232 if (tmpList.count() > 1)
234 addressee.setGivenName(tmpList[1]); 233 addressee.setGivenName(tmpList[1]);
235 if (tmpList.count() > 2) 234 if (tmpList.count() > 2)
236 addressee.setAdditionalName(tmpList[2]); 235 addressee.setAdditionalName(tmpList[2]);
237 if (tmpList.count() > 3) 236 if (tmpList.count() > 3)
238 addressee.setPrefix(tmpList[3]); 237 addressee.setPrefix(tmpList[3]);
239 if (tmpList.count() > 4) 238 if (tmpList.count() > 4)
240 addressee.setSuffix(tmpList[4]); 239 addressee.setSuffix(tmpList[4]);
241 240
242 tmpStr = (mVCard->getValue(VCARD_FN)); 241 tmpStr = (mVCard->getValue(VCARD_FN));
243 if (!tmpStr.isEmpty()) 242 if (!tmpStr.isEmpty())
244 addressee.setFormattedName(tmpStr); 243 addressee.setFormattedName(tmpStr);
245 244
246 //set the addressee's nick name 245 //set the addressee's nick name
247 tmpStr = mVCard->getValue(VCARD_NICKNAME); 246 tmpStr = mVCard->getValue(VCARD_NICKNAME);
248 addressee.setNickName(tmpStr); 247 addressee.setNickName(tmpStr);
249 //set the addressee's organisation 248 //set the addressee's organisation
250 tmpStr = mVCard->getValue(VCARD_ORG); 249 tmpStr = mVCard->getValue(VCARD_ORG);
251 addressee.setOrganization(tmpStr); 250 addressee.setOrganization(tmpStr);
252 //set the addressee's title 251 //set the addressee's title
253 tmpStr = mVCard->getValue(VCARD_TITLE); 252 tmpStr = mVCard->getValue(VCARD_TITLE);
254 addressee.setTitle(tmpStr); 253 addressee.setTitle(tmpStr);
255 //set the addressee's email - we can only deal with two. The preferenced one and one other. 254 //set the addressee's email - we can only deal with two. The preferenced one and one other.
256 tmpStr = mVCard->getValue(VCARD_EMAIL, VCARD_EMAIL_INTERNET); 255 tmpStr = mVCard->getValue(VCARD_EMAIL, VCARD_EMAIL_INTERNET);
257 addressee.insertEmail(tmpStr, false); 256 addressee.insertEmail(tmpStr, false);
258 tmpStr = mVCard->getValue(VCARD_EMAIL,VCARD_EMAIL_PREF); 257 tmpStr = mVCard->getValue(VCARD_EMAIL,VCARD_EMAIL_PREF);
259 addressee.insertEmail(tmpStr, true); 258 addressee.insertEmail(tmpStr, true);
260 //set the addressee's url 259 //set the addressee's url
261 tmpStr = mVCard->getValue(VCARD_URL); 260 tmpStr = mVCard->getValue(VCARD_URL);
262 if (tmpStr.isEmpty()) tmpStr = mVCard->getValue(VCARD_URL, VCARD_ADR_WORK); 261 if (tmpStr.isEmpty()) tmpStr = mVCard->getValue(VCARD_URL, VCARD_ADR_WORK);
263 if (tmpStr.isEmpty()) tmpStr = mVCard->getValue(VCARD_URL, VCARD_ADR_HOME); 262 if (tmpStr.isEmpty()) tmpStr = mVCard->getValue(VCARD_URL, VCARD_ADR_HOME);
264 if (!tmpStr.isEmpty()) { 263 if (!tmpStr.isEmpty()) {
265 addressee.setUrl(KURL(tmpStr)); 264 addressee.setUrl(KURL(tmpStr));
266 } 265 }
267 266
268 //set the addressee's birthday 267 //set the addressee's birthday
269 tmpStr = mVCard->getValue(VCARD_BDAY); 268 tmpStr = mVCard->getValue(VCARD_BDAY);
270 addressee.setBirthday(VCardStringToDate(tmpStr)); 269 addressee.setBirthday(VCardStringToDate(tmpStr));
271 270
272 //set the addressee's phone numbers 271 //set the addressee's phone numbers
273 for ( QValueListIterator<VCardLineX> i = mVCard->_vcdata->begin();i != mVCard->_vcdata->end(); ++i ) { 272 for ( QValueListIterator<VCardLineX> i = mVCard->_vcdata->begin();i != mVCard->_vcdata->end(); ++i ) {
274 if ( (*i).name == VCARD_TEL ) { 273 if ( (*i).name == VCARD_TEL ) {
275 int type = 0; 274 int type = 0;
276 if ( (*i).qualified ) { 275 if ( (*i).qualified ) {
277 if ( (*i).qualifiers.contains( VCARD_TEL_HOME ) ) 276 if ( (*i).qualifiers.contains( VCARD_TEL_HOME ) )
278 type |= PhoneNumber::Home; 277 type |= PhoneNumber::Home;
279 if ( (*i).qualifiers.contains( VCARD_TEL_WORK ) ) 278 if ( (*i).qualifiers.contains( VCARD_TEL_WORK ) )
280 type |= PhoneNumber::Work; 279 type |= PhoneNumber::Work;
281 if ( (*i).qualifiers.contains( VCARD_TEL_PREF ) ) 280 if ( (*i).qualifiers.contains( VCARD_TEL_PREF ) )
282 type |= PhoneNumber::Pref; 281 type |= PhoneNumber::Pref;
283 // if ( (*i).qualifiers.contains( VCARD_TEL_VOICE ) ) 282 if ( (*i).qualifiers.contains( VCARD_TEL_VOICE ) )
284 // type |= PhoneNumber::Voice; 283 type |= PhoneNumber::Voice;
285 if ( (*i).qualifiers.contains( VCARD_TEL_FAX ) ) 284 if ( (*i).qualifiers.contains( VCARD_TEL_FAX ) )
286 type |= PhoneNumber::Fax; 285 type |= PhoneNumber::Fax;
287 if ( (*i).qualifiers.contains( VCARD_TEL_MSG ) ) 286 if ( (*i).qualifiers.contains( VCARD_TEL_MSG ) )
288 type |= PhoneNumber::Msg; 287 type |= PhoneNumber::Msg;
289 if ( (*i).qualifiers.contains( VCARD_TEL_CELL ) ) 288 if ( (*i).qualifiers.contains( VCARD_TEL_CELL ) )
290 type |= PhoneNumber::Cell; 289 type |= PhoneNumber::Cell;
291 if ( (*i).qualifiers.contains( VCARD_TEL_PAGER ) ) 290 if ( (*i).qualifiers.contains( VCARD_TEL_PAGER ) )
292 type |= PhoneNumber::Pager; 291 type |= PhoneNumber::Pager;
293 if ( (*i).qualifiers.contains( VCARD_TEL_BBS ) ) 292 if ( (*i).qualifiers.contains( VCARD_TEL_BBS ) )
294 type |= PhoneNumber::Bbs; 293 type |= PhoneNumber::Bbs;
295 if ( (*i).qualifiers.contains( VCARD_TEL_MODEM ) ) 294 if ( (*i).qualifiers.contains( VCARD_TEL_MODEM ) )
296 type |= PhoneNumber::Modem; 295 type |= PhoneNumber::Modem;
297 if ( (*i).qualifiers.contains( VCARD_TEL_CAR ) ) 296 if ( (*i).qualifiers.contains( VCARD_TEL_CAR ) )
298 type |= PhoneNumber::Car; 297 type |= PhoneNumber::Car;
299 if ( (*i).qualifiers.contains( VCARD_TEL_ISDN ) ) 298 if ( (*i).qualifiers.contains( VCARD_TEL_ISDN ) )
300 type |= PhoneNumber::Isdn; 299 type |= PhoneNumber::Isdn;
301 if ( (*i).qualifiers.contains( VCARD_TEL_VIDEO ) ) 300 if ( (*i).qualifiers.contains( VCARD_TEL_VIDEO ) )
302 type |= PhoneNumber::Video; 301 type |= PhoneNumber::Video;
303 if ( (*i).qualifiers.contains( VCARD_TEL_PCS ) ) 302 if ( (*i).qualifiers.contains( VCARD_TEL_PCS ) )
304 type |= PhoneNumber::Pcs; 303 type |= PhoneNumber::Pcs;
305 if ( (*i).qualifiers.contains( VCARD_TEL_SIP ) ) 304
306 type |= PhoneNumber::Sip;
307 } 305 }
308 addressee.insertPhoneNumber( PhoneNumber( (*i).parameters[ 0 ], type ) ); 306 addressee.insertPhoneNumber( PhoneNumber( (*i).parameters[ 0 ], type ) );
309 } 307 }
310 } 308 }
311 309 addressee.makePhoneNumbersOLcompatible();
312 //set the addressee's addresses 310 //set the addressee's addresses
313 for ( QValueListIterator<VCardLineX> i = mVCard->_vcdata->begin();i != mVCard->_vcdata->end(); ++i ) { 311 for ( QValueListIterator<VCardLineX> i = mVCard->_vcdata->begin();i != mVCard->_vcdata->end(); ++i ) {
314 if ( (*i).name == VCARD_ADR ) { 312 if ( (*i).name == VCARD_ADR ) {
315 int type = 0; 313 int type = 0;
316 if ( (*i).qualified ) { 314 if ( (*i).qualified ) {
317 if ( (*i).qualifiers.contains( VCARD_ADR_DOM ) ) 315 if ( (*i).qualifiers.contains( VCARD_ADR_DOM ) )
318 type |= Address::Dom; 316 type |= Address::Dom;
319 if ( (*i).qualifiers.contains( VCARD_ADR_INTL ) ) 317 if ( (*i).qualifiers.contains( VCARD_ADR_INTL ) )
320 type |= Address::Intl; 318 type |= Address::Intl;
321 if ( (*i).qualifiers.contains( VCARD_ADR_POSTAL ) ) 319 if ( (*i).qualifiers.contains( VCARD_ADR_POSTAL ) )
322 type |= Address::Postal; 320 type |= Address::Postal;
323 if ( (*i).qualifiers.contains( VCARD_ADR_PARCEL ) ) 321 if ( (*i).qualifiers.contains( VCARD_ADR_PARCEL ) )
324 type |= Address::Parcel; 322 type |= Address::Parcel;
325 if ( (*i).qualifiers.contains( VCARD_ADR_HOME ) ) 323 if ( (*i).qualifiers.contains( VCARD_ADR_HOME ) )
326 type |= Address::Home; 324 type |= Address::Home;
327 if ( (*i).qualifiers.contains( VCARD_ADR_WORK ) ) 325 if ( (*i).qualifiers.contains( VCARD_ADR_WORK ) )
328 type |= Address::Work; 326 type |= Address::Work;
329 if ( (*i).qualifiers.contains( VCARD_ADR_PREF ) ) 327 if ( (*i).qualifiers.contains( VCARD_ADR_PREF ) )
330 type |= Address::Pref; 328 type |= Address::Pref;
331 } 329 }
332 addressee.insertAddress( readAddressFromQStringList( (*i).parameters, type ) ); 330 addressee.insertAddress( readAddressFromQStringList( (*i).parameters, type ) );
333 } 331 }
334 } 332 }
335 333
336 //set the addressee's delivery label 334 //set the addressee's delivery label
337 tmpStr = mVCard->getValue(VCARD_LABEL); 335 tmpStr = mVCard->getValue(VCARD_LABEL);
338 if (!tmpStr.isEmpty()) { 336 if (!tmpStr.isEmpty()) {
339qDebug("VCard21Parser::readFromString please verify if replace is correct"); 337qDebug("VCard21Parser::readFromString please verify if replace is correct");
340//US tmpStr.replace("\r\n","\n"); 338//US tmpStr.replace("\r\n","\n");
341 tmpStr.replace( QRegExp("\r\n"), "\n" ); 339 tmpStr.replace( QRegExp("\r\n"), "\n" );
342 Address tmpAddress; 340 Address tmpAddress;
343 tmpAddress.setLabel(tmpStr); 341 tmpAddress.setLabel(tmpStr);
344 addressee.insertAddress(tmpAddress); 342 addressee.insertAddress(tmpAddress);
345 } 343 }
346 344
347 //set the addressee's notes 345 //set the addressee's notes
348 tmpStr = mVCard->getValue(VCARD_NOTE); 346 tmpStr = mVCard->getValue(VCARD_NOTE);
349qDebug("VCard21Parser::readFromString please verify if correct"); 347qDebug("VCard21Parser::readFromString please verify if correct");
350//US tmpStr.replace("\r\n","\n"); 348//US tmpStr.replace("\r\n","\n");
351 tmpStr.replace( QRegExp("\r\n"), "\n" ); 349 tmpStr.replace( QRegExp("\r\n"), "\n" );
352 addressee.setNote(tmpStr); 350 addressee.setNote(tmpStr);
353 351
354 //set the addressee's timezone 352 //set the addressee's timezone
355 tmpStr = mVCard->getValue(VCARD_TZ); 353 tmpStr = mVCard->getValue(VCARD_TZ);
356 TimeZone tmpZone(tmpStr.toInt()); 354 TimeZone tmpZone(tmpStr.toInt());
357 addressee.setTimeZone(tmpZone); 355 addressee.setTimeZone(tmpZone);
358 356
359 //set the addressee's geographical position 357 //set the addressee's geographical position
360 tmpList = mVCard->getValues(VCARD_GEO); 358 tmpList = mVCard->getValues(VCARD_GEO);
361 if (tmpList.count()==2) 359 if (tmpList.count()==2)
362 { 360 {
363 tmpStr = tmpList[0]; 361 tmpStr = tmpList[0];
364 float glat = tmpStr.toFloat(); 362 float glat = tmpStr.toFloat();
365 tmpStr = tmpList[1]; 363 tmpStr = tmpList[1];
366 float glong = tmpStr.toFloat(); 364 float glong = tmpStr.toFloat();
367 Geo tmpGeo(glat,glong); 365 Geo tmpGeo(glat,glong);
368 addressee.setGeo(tmpGeo); 366 addressee.setGeo(tmpGeo);
369 } 367 }
370 368
371 //set the last revision date 369 //set the last revision date
372 tmpStr = mVCard->getValue(VCARD_REV); 370 tmpStr = mVCard->getValue(VCARD_REV);
373 addressee.setRevision(VCardStringToDate(tmpStr)); 371 addressee.setRevision(VCardStringToDate(tmpStr));
374 372
375 //set the role of the addressee 373 //set the role of the addressee
376 tmpStr = mVCard->getValue(VCARD_ROLE); 374 tmpStr = mVCard->getValue(VCARD_ROLE);
377 addressee.setRole(tmpStr); 375 addressee.setRole(tmpStr);
378 376
379 return addressee; 377 return addressee;
380} 378}
381 379
382 380
383 381
384KABC::Address VCard21Parser::readAddressFromQStringList ( const QStringList &data, const int type ) 382KABC::Address VCard21Parser::readAddressFromQStringList ( const QStringList &data, const int type )
385{ 383{
386 KABC::Address mAddress; 384 KABC::Address mAddress;
387 mAddress.setType( type ); 385 mAddress.setType( type );
388 386
389 if ( data.count() > 0 ) 387 if ( data.count() > 0 )
390 mAddress.setPostOfficeBox( data[0] ); 388 mAddress.setPostOfficeBox( data[0] );
391 if ( data.count() > 1 ) 389 if ( data.count() > 1 )
392 mAddress.setExtended( data[1] ); 390 mAddress.setExtended( data[1] );
393 if ( data.count() > 2 ) 391 if ( data.count() > 2 )
394 mAddress.setStreet( data[2] ); 392 mAddress.setStreet( data[2] );
395 if ( data.count() > 3 ) 393 if ( data.count() > 3 )
396 mAddress.setLocality( data[3] ); 394 mAddress.setLocality( data[3] );
397 if ( data.count() > 4 ) 395 if ( data.count() > 4 )
398 mAddress.setRegion( data[4] ); 396 mAddress.setRegion( data[4] );
399 if ( data.count() > 5 ) 397 if ( data.count() > 5 )
400 mAddress.setPostalCode( data[5] ); 398 mAddress.setPostalCode( data[5] );
401 if ( data.count() > 6 ) 399 if ( data.count() > 6 )
402 mAddress.setCountry( data[6] ); 400 mAddress.setCountry( data[6] );
403 401
404 return mAddress; 402 return mAddress;
405} 403}
406 404
407 405
408VCard21ParserImpl *VCard21ParserImpl::parseVCard( const QString& vc, int *err ) 406VCard21ParserImpl *VCard21ParserImpl::parseVCard( const QString& vc, int *err )
409{ 407{
410 int _err = 0; 408 int _err = 0;
411 int _state = VC_STATE_BEGIN; 409 int _state = VC_STATE_BEGIN;
412 410
413 QValueList<VCardLineX> *_vcdata; 411 QValueList<VCardLineX> *_vcdata;
414 QValueList<QString> lines; 412 QValueList<QString> lines;
415 413
416 _vcdata = new QValueList<VCardLineX>; 414 _vcdata = new QValueList<VCardLineX>;
417 415
418 lines = QStringList::split( QRegExp( "[\x0d\x0a]" ), vc ); 416 lines = QStringList::split( QRegExp( "[\x0d\x0a]" ), vc );
419 417
420 // for each line in the vCard 418 // for each line in the vCard
421 for ( QStringList::Iterator j = lines.begin(); j != lines.end(); ++j ) { 419 for ( QStringList::Iterator j = lines.begin(); j != lines.end(); ++j ) {
422 VCardLineX _vcl; 420 VCardLineX _vcl;
423 421
424 // take spaces off the end - ugly but necessary hack 422 // take spaces off the end - ugly but necessary hack
425 for ( int g = (*j).length()-1; g > 0 && (*j)[g].isSpace(); --g ) 423 for ( int g = (*j).length()-1; g > 0 && (*j)[g].isSpace(); --g )
426 (*j)[g] = 0; 424 (*j)[g] = 0;
427 425
428 // first token: 426 // first token:
429 // verify state, update if necessary 427 // verify state, update if necessary
430 if ( _state & VC_STATE_BEGIN) { 428 if ( _state & VC_STATE_BEGIN) {
431 if ( !qstricmp( (*j).latin1(), VCARD_BEGIN ) ) { 429 if ( !qstricmp( (*j).latin1(), VCARD_BEGIN ) ) {
432 _state = VC_STATE_BODY; 430 _state = VC_STATE_BODY;
433 continue; 431 continue;
434 } else { 432 } else {
435 _err = VC_ERR_NO_BEGIN; 433 _err = VC_ERR_NO_BEGIN;
436 break; 434 break;
437 } 435 }
438 } else if ( _state & VC_STATE_BODY ) { 436 } else if ( _state & VC_STATE_BODY ) {
439 if ( !qstricmp( (*j).latin1(), VCARD_END ) ) { 437 if ( !qstricmp( (*j).latin1(), VCARD_END ) ) {
440 _state |= VC_STATE_END; 438 _state |= VC_STATE_END;
441 break; 439 break;
442 } 440 }
443 441
444 // split into two tokens 442 // split into two tokens
445 int colon = (*j).find( ':' ); 443 int colon = (*j).find( ':' );
446 if ( colon < 0 ) { 444 if ( colon < 0 ) {
447 _err = VC_ERR_INVALID_LINE; 445 _err = VC_ERR_INVALID_LINE;
448 break; 446 break;
449 } 447 }
450 448
451 QString key = (*j).left( colon ); 449 QString key = (*j).left( colon );
452 QString value = (*j).mid( colon + 1 ); 450 QString value = (*j).mid( colon + 1 );
453 451
454 // check for qualifiers and 452 // check for qualifiers and
455 // set name, qualified, qualifier(s) 453 // set name, qualified, qualifier(s)
456 QStringList keyTokens = QStringList::split( ';', key ); 454 QStringList keyTokens = QStringList::split( ';', key );
457 bool qp = false, first_pass = true; 455 bool qp = false, first_pass = true;
458 bool b64 = false; 456 bool b64 = false;
459 457
460 if ( keyTokens.count() > 0 ) { 458 if ( keyTokens.count() > 0 ) {
461 _vcl.qualified = false; 459 _vcl.qualified = false;
462 _vcl.name = keyTokens[ 0 ].lower(); 460 _vcl.name = keyTokens[ 0 ].lower();
463 461
464 for ( QStringList::Iterator z = keyTokens.begin(); z != keyTokens.end(); ++z ) { 462 for ( QStringList::Iterator z = keyTokens.begin(); z != keyTokens.end(); ++z ) {
465 QString zz = (*z).lower(); 463 QString zz = (*z).lower();
466 if ( zz == VCARD_QUOTED_PRINTABLE || zz == VCARD_ENCODING_QUOTED_PRINTABLE ) { 464 if ( zz == VCARD_QUOTED_PRINTABLE || zz == VCARD_ENCODING_QUOTED_PRINTABLE ) {
467 qp = true; 465 qp = true;
468 } else if ( zz == VCARD_BASE64 ) { 466 } else if ( zz == VCARD_BASE64 ) {
469 b64 = true; 467 b64 = true;
470 } else if ( !first_pass ) { 468 } else if ( !first_pass ) {
471 _vcl.qualified = true; 469 _vcl.qualified = true;
472 _vcl.qualifiers.append( zz ); 470 _vcl.qualifiers.append( zz );
473 } 471 }
474 first_pass = false; 472 first_pass = false;
475 } 473 }
476 } else { 474 } else {
477 _err = VC_ERR_INVALID_LINE; 475 _err = VC_ERR_INVALID_LINE;
478 } 476 }
479 477
480 if ( _err != 0 ) 478 if ( _err != 0 )
481 break; 479 break;
482 480
483 if ( _vcl.name == VCARD_VERSION ) 481 if ( _vcl.name == VCARD_VERSION )
484 _state |= VC_STATE_HAVE_VERSION; 482 _state |= VC_STATE_HAVE_VERSION;
485 483
486 if ( _vcl.name == VCARD_N || _vcl.name == VCARD_FN ) 484 if ( _vcl.name == VCARD_N || _vcl.name == VCARD_FN )
487 _state |= VC_STATE_HAVE_N; 485 _state |= VC_STATE_HAVE_N;
488 486
489 // second token: 487 // second token:
490 // split into tokens by ; 488 // split into tokens by ;
491 // add to parameters vector 489 // add to parameters vector
492 if ( b64 ) { 490 if ( b64 ) {
493 if ( value.at( value.length() - 1 ) != '=' ) 491 if ( value.at( value.length() - 1 ) != '=' )
494 do { 492 do {
495 value += *( ++j ); 493 value += *( ++j );
496 } while ( (*j).at( (*j).length() - 1 ) != '=' ); 494 } while ( (*j).at( (*j).length() - 1 ) != '=' );
497 } else { 495 } else {
498 if ( qp ) { // join any split lines 496 if ( qp ) { // join any split lines
499 while ( value.at( value.length() - 1 ) == '=' ) { 497 while ( value.at( value.length() - 1 ) == '=' ) {
500 value.remove( value.length() - 1, 1 ); 498 value.remove( value.length() - 1, 1 );
501 value.append(*( ++j )); 499 value.append(*( ++j ));
502 } 500 }
503 } 501 }
504 _vcl.parameters = QStringList::split( ';', value, true ); 502 _vcl.parameters = QStringList::split( ';', value, true );
505 if ( qp ) { // decode the quoted printable 503 if ( qp ) { // decode the quoted printable
506 for ( QStringList::Iterator z = _vcl.parameters.begin(); z != _vcl.parameters.end(); ++z ) 504 for ( QStringList::Iterator z = _vcl.parameters.begin(); z != _vcl.parameters.end(); ++z )
507 *z = KCodecs::quotedPrintableDecode( (*z).latin1() ); 505 *z = KCodecs::quotedPrintableDecode( (*z).latin1() );
508 } 506 }
509 } 507 }
510 } else { 508 } else {
511 _err = VC_ERR_INTERNAL; 509 _err = VC_ERR_INTERNAL;
512 break; 510 break;
513 } 511 }
514 512
515 // validate VCardLineX 513 // validate VCardLineX
516 if ( !_vcl.isValid() ) { 514 if ( !_vcl.isValid() ) {
517 _err = VC_ERR_INVALID_LINE; 515 _err = VC_ERR_INVALID_LINE;
518 break; 516 break;
519 } 517 }
520 518
521 // add to vector 519 // add to vector
522 _vcdata->append( _vcl ); 520 _vcdata->append( _vcl );
523 } 521 }
524 522
525 // errors to check at the last minute (exit state related) 523 // errors to check at the last minute (exit state related)
526 if ( _err == 0 ) { 524 if ( _err == 0 ) {
527 if ( !( _state & VC_STATE_END ) ) // we have to have an end!! 525 if ( !( _state & VC_STATE_END ) ) // we have to have an end!!
528 _err = VC_ERR_NO_END; 526 _err = VC_ERR_NO_END;
529 527
530 if ( !( _state & VC_STATE_HAVE_N ) || // we have to have the mandatories! 528 if ( !( _state & VC_STATE_HAVE_N ) || // we have to have the mandatories!
531 !( _state & VC_STATE_HAVE_VERSION ) ) 529 !( _state & VC_STATE_HAVE_VERSION ) )
532 _err = VC_ERR_MISSING_MANDATORY; 530 _err = VC_ERR_MISSING_MANDATORY;
533 } 531 }
534 532
535 // set the error message if we can, and only return an object 533 // set the error message if we can, and only return an object
536 // if the vCard was valid. 534 // if the vCard was valid.
537 if ( err ) 535 if ( err )
538 *err = _err; 536 *err = _err;
539 537
540 if ( _err != 0 ) { 538 if ( _err != 0 ) {
541 delete _vcdata; 539 delete _vcdata;
542 return 0; 540 return 0;
543 } 541 }
544 542
545 return new VCard21ParserImpl( _vcdata ); 543 return new VCard21ParserImpl( _vcdata );
546} 544}
547 545
548VCard21ParserImpl::VCard21ParserImpl(QValueList<VCardLineX> *_vcd) : _vcdata(_vcd) 546VCard21ParserImpl::VCard21ParserImpl(QValueList<VCardLineX> *_vcd) : _vcdata(_vcd)
549{ 547{
550} 548}
551 549
552 550
553QString VCard21ParserImpl::getValue(const QString& name, const QString& qualifier) 551QString VCard21ParserImpl::getValue(const QString& name, const QString& qualifier)
554{ 552{
555 QString failed; 553 QString failed;
556 const QString lowname = name.lower(); 554 const QString lowname = name.lower();
557 const QString lowqualifier = qualifier.lower(); 555 const QString lowqualifier = qualifier.lower();
558 556
559 for (QValueListIterator<VCardLineX> i = _vcdata->begin();i != _vcdata->end();++i) { 557 for (QValueListIterator<VCardLineX> i = _vcdata->begin();i != _vcdata->end();++i) {
560 if ((*i).name == lowname && (*i).qualified && (*i).qualifiers.contains(lowqualifier)) { 558 if ((*i).name == lowname && (*i).qualified && (*i).qualifiers.contains(lowqualifier)) {
561 if ((*i).parameters.count() > 0) 559 if ((*i).parameters.count() > 0)
562 return (*i).parameters[0]; 560 return (*i).parameters[0];
563 else return failed; 561 else return failed;
564 } 562 }
565 } 563 }
566 return failed; 564 return failed;
567} 565}
568 566
569 567
570QString VCard21ParserImpl::getValue(const QString& name) 568QString VCard21ParserImpl::getValue(const QString& name)
571{ 569{
572 QString failed; 570 QString failed;
573 const QString lowname = name.lower(); 571 const QString lowname = name.lower();
574 572
575 for (QValueListIterator<VCardLineX> i = _vcdata->begin();i != _vcdata->end();++i) { 573 for (QValueListIterator<VCardLineX> i = _vcdata->begin();i != _vcdata->end();++i) {
576 if ((*i).name == lowname && !(*i).qualified) { 574 if ((*i).name == lowname && !(*i).qualified) {
577 if ((*i).parameters.count() > 0) 575 if ((*i).parameters.count() > 0)
578 return (*i).parameters[0]; 576 return (*i).parameters[0];
579 else return failed; 577 else return failed;
580 } 578 }
581 } 579 }
582 return failed; 580 return failed;
583} 581}
584 582
585 583
586QStringList VCard21ParserImpl::getValues(const QString& name) 584QStringList VCard21ParserImpl::getValues(const QString& name)
587{ 585{
588 const QString lowname = name.lower(); 586 const QString lowname = name.lower();
589 for (QValueListIterator<VCardLineX> i = _vcdata->begin();i != _vcdata->end();++i) { 587 for (QValueListIterator<VCardLineX> i = _vcdata->begin();i != _vcdata->end();++i) {
590 if ((*i).name == lowname && !(*i).qualified) 588 if ((*i).name == lowname && !(*i).qualified)
591 return (*i).parameters; 589 return (*i).parameters;
592 } 590 }
593 // failed. 591 // failed.
594 return QStringList(); 592 return QStringList();
595} 593}
596 594
597QStringList VCard21ParserImpl::getValues(const QString& name, const QString& qualifier) 595QStringList VCard21ParserImpl::getValues(const QString& name, const QString& qualifier)
598{ 596{
599 const QString lowname = name.lower(); 597 const QString lowname = name.lower();
600 const QString lowqualifier = qualifier.lower(); 598 const QString lowqualifier = qualifier.lower();
601 for (QValueListIterator<VCardLineX> i = _vcdata->begin();i != _vcdata->end();++i) { 599 for (QValueListIterator<VCardLineX> i = _vcdata->begin();i != _vcdata->end();++i) {
602 if ((*i).name == lowname && (*i).qualified && (*i).qualifiers.contains(lowqualifier)) 600 if ((*i).name == lowname && (*i).qualified && (*i).qualifiers.contains(lowqualifier))
603 return (*i).parameters; 601 return (*i).parameters;
604 } 602 }
605 // failed. 603 // failed.
606 return QStringList(); 604 return QStringList();
607} 605}
608 606
609 607