25 #include "vcard21parser.h"
26 #include "vcardconverter.h"
30 bool VCardLineX::isValid()
const
33 if ( name == VCARD_BEGIN_N || name == VCARD_END_N )
36 if ( name[0] ==
'x' && name[1] ==
'-' )
43 if ( name == VCARD_ADR && qualified &&
44 (qualifiers.contains(VCARD_ADR_DOM) ||
45 qualifiers.contains(VCARD_ADR_INTL) ||
46 qualifiers.contains(VCARD_ADR_POSTAL) ||
47 qualifiers.contains(VCARD_ADR_HOME) ||
48 qualifiers.contains(VCARD_ADR_WORK) ||
49 qualifiers.contains(VCARD_ADR_PREF)
53 if ( name == VCARD_AGENT )
58 if ( name == VCARD_BDAY )
63 if ( name == VCARD_CATEGORIES )
65 if ( name == VCARD_CLASS && qualified &&
66 (qualifiers.contains(VCARD_CLASS_PUBLIC) ||
67 qualifiers.contains(VCARD_CLASS_PRIVATE) ||
68 qualifiers.contains(VCARD_CLASS_CONFIDENTIAL)
74 if ( name == VCARD_EMAIL && qualified &&
75 (qualifiers.contains(VCARD_EMAIL_INTERNET) ||
76 qualifiers.contains(VCARD_EMAIL_PREF) ||
77 qualifiers.contains(VCARD_EMAIL_X400)
83 if ( name == VCARD_FN )
88 if ( name == VCARD_GEO )
93 if ( name == VCARD_KEY && qualified &&
94 (qualifiers.contains(VCARD_KEY_X509) ||
95 qualifiers.contains(VCARD_KEY_PGP)
101 if ( name == VCARD_LABEL )
103 if ( name == VCARD_LOGO )
108 if ( name == VCARD_MAILER )
113 if ( name == VCARD_N )
115 if ( name == VCARD_NAME )
117 if ( name == VCARD_NICKNAME )
119 if ( name == VCARD_NOTE )
124 if ( name == VCARD_ORG )
129 if ( name == VCARD_PHOTO )
131 if ( name == VCARD_PROFILE )
133 if ( name == VCARD_PRODID )
138 if ( name == VCARD_ROLE )
140 if ( name == VCARD_REV )
145 if ( name == VCARD_SOURCE )
147 if ( name == VCARD_SOUND )
152 if ( name == VCARD_TEL && qualified &&
153 (qualifiers.contains(VCARD_TEL_HOME) ||
154 qualifiers.contains(VCARD_TEL_WORK) ||
155 qualifiers.contains(VCARD_TEL_PREF) ||
156 qualifiers.contains(VCARD_TEL_VOICE) ||
157 qualifiers.contains(VCARD_TEL_FAX) ||
158 qualifiers.contains(VCARD_TEL_MSG) ||
159 qualifiers.contains(VCARD_TEL_CELL) ||
160 qualifiers.contains(VCARD_TEL_PAGER) ||
161 qualifiers.contains(VCARD_TEL_BBS) ||
162 qualifiers.contains(VCARD_TEL_MODEM) ||
163 qualifiers.contains(VCARD_TEL_CAR) ||
164 qualifiers.contains(VCARD_TEL_ISDN) ||
165 qualifiers.contains(VCARD_TEL_VIDEO) ||
166 qualifiers.contains(VCARD_TEL_PCS)
169 if ( name == VCARD_TZ )
171 if ( name == VCARD_TITLE )
176 if ( name == VCARD_URL )
178 if ( name == VCARD_UID )
183 if ( name == VCARD_VERSION )
221 TQStringList tmpList = vCard->getValues(VCARD_N);
222 TQString formattedName =
"";
223 if (tmpList.count() > 0)
224 addressee.setFamilyName(tmpList[0]);
225 if (tmpList.count() > 1)
226 addressee.setGivenName(tmpList[1]);
227 if (tmpList.count() > 2)
228 addressee.setAdditionalName(tmpList[2]);
229 if (tmpList.count() > 3)
230 addressee.setPrefix(tmpList[3]);
231 if (tmpList.count() > 4)
232 addressee.setSuffix(tmpList[4]);
234 tmpStr = (vCard->getValue(VCARD_FN));
235 if (!tmpStr.isEmpty())
236 addressee.setFormattedName(tmpStr);
239 tmpStr = vCard->getValue(VCARD_NICKNAME);
240 addressee.setNickName(tmpStr);
242 tmpStr = vCard->getValue(VCARD_ORG);
243 addressee.setOrganization(tmpStr);
245 tmpStr = vCard->getValue(VCARD_TITLE);
246 addressee.setTitle(tmpStr);
248 tmpStr = vCard->getValue(VCARD_EMAIL, VCARD_EMAIL_INTERNET);
250 tmpStr = vCard->getValue(VCARD_EMAIL,VCARD_EMAIL_PREF);
253 tmpStr = vCard->getValue(VCARD_URL);
254 if (tmpStr.isEmpty()) tmpStr = vCard->getValue(VCARD_URL, VCARD_ADR_WORK);
255 if (tmpStr.isEmpty()) tmpStr = vCard->getValue(VCARD_URL, VCARD_ADR_HOME);
256 if (!tmpStr.isEmpty()) {
257 addressee.setUrl(
KURL(tmpStr));
261 tmpStr = vCard->getValue(VCARD_BDAY);
265 for ( TQValueListIterator<VCardLineX> i = vCard->_vcdata->begin();i != vCard->_vcdata->end(); ++i ) {
266 if ( (*i).name == VCARD_TEL ) {
268 if ( (*i).qualified ) {
269 if ( (*i).qualifiers.contains( VCARD_TEL_HOME ) )
270 type |= PhoneNumber::Home;
271 if ( (*i).qualifiers.contains( VCARD_TEL_WORK ) )
272 type |= PhoneNumber::Work;
273 if ( (*i).qualifiers.contains( VCARD_TEL_PREF ) )
274 type |= PhoneNumber::Pref;
277 if ( (*i).qualifiers.contains( VCARD_TEL_FAX ) )
278 type |= PhoneNumber::Fax;
279 if ( (*i).qualifiers.contains( VCARD_TEL_MSG ) )
280 type |= PhoneNumber::Msg;
281 if ( (*i).qualifiers.contains( VCARD_TEL_CELL ) )
282 type |= PhoneNumber::Cell;
283 if ( (*i).qualifiers.contains( VCARD_TEL_PAGER ) )
284 type |= PhoneNumber::Pager;
285 if ( (*i).qualifiers.contains( VCARD_TEL_BBS ) )
286 type |= PhoneNumber::Bbs;
287 if ( (*i).qualifiers.contains( VCARD_TEL_MODEM ) )
288 type |= PhoneNumber::Modem;
289 if ( (*i).qualifiers.contains( VCARD_TEL_CAR ) )
290 type |= PhoneNumber::Car;
291 if ( (*i).qualifiers.contains( VCARD_TEL_ISDN ) )
292 type |= PhoneNumber::Isdn;
293 if ( (*i).qualifiers.contains( VCARD_TEL_VIDEO ) )
294 type |= PhoneNumber::Video;
295 if ( (*i).qualifiers.contains( VCARD_TEL_PCS ) )
296 type |= PhoneNumber::Pcs;
303 for ( TQValueListIterator<VCardLineX> i = vCard->_vcdata->begin();i != vCard->_vcdata->end(); ++i ) {
304 if ( (*i).name == VCARD_ADR ) {
306 if ( (*i).qualified ) {
307 if ( (*i).qualifiers.contains( VCARD_ADR_DOM ) )
308 type |= Address::Dom;
309 if ( (*i).qualifiers.contains( VCARD_ADR_INTL ) )
310 type |= Address::Intl;
311 if ( (*i).qualifiers.contains( VCARD_ADR_POSTAL ) )
312 type |= Address::Postal;
313 if ( (*i).qualifiers.contains( VCARD_ADR_PARCEL ) )
314 type |= Address::Parcel;
315 if ( (*i).qualifiers.contains( VCARD_ADR_HOME ) )
316 type |= Address::Home;
317 if ( (*i).qualifiers.contains( VCARD_ADR_WORK ) )
318 type |= Address::Work;
319 if ( (*i).qualifiers.contains( VCARD_ADR_PREF ) )
320 type |= Address::Pref;
327 tmpStr = vCard->getValue(VCARD_LABEL);
328 if (!tmpStr.isEmpty()) {
329 tmpStr.replace(
"\r\n",
"\n");
336 tmpStr = vCard->getValue(VCARD_NOTE);
337 tmpStr.replace(
"\r\n",
"\n");
338 addressee.setNote(tmpStr);
341 tmpStr = vCard->getValue(VCARD_TZ);
343 addressee.setTimeZone(tmpZone);
346 tmpList = vCard->getValues(VCARD_GEO);
347 if (tmpList.count()==2)
350 float glat = tmpStr.toFloat();
352 float glong = tmpStr.toFloat();
353 Geo tmpGeo(glat,glong);
354 addressee.setGeo(tmpGeo);
358 tmpStr = vCard->getValue(VCARD_REV);
362 tmpStr = vCard->getValue(VCARD_ROLE);
363 addressee.setRole(tmpStr);
377 if ( data.count() > 0 )
379 if ( data.count() > 1 )
381 if ( data.count() > 2 )
383 if ( data.count() > 3 )
385 if ( data.count() > 4 )
387 if ( data.count() > 5 )
389 if ( data.count() > 6 )
399 int _state = VC_STATE_BEGIN;
401 TQValueList<VCardLineX> *vcdata;
402 TQValueList<TQString> lines;
404 vcdata =
new TQValueList<VCardLineX>;
406 lines = TQStringList::split( TQRegExp(
"[\x0d\x0a]" ), vc );
409 for ( TQStringList::Iterator j = lines.begin(); j != lines.end(); ++j ) {
413 for (
int g = (*j).length()-1; g > 0 && (*j)[g].isSpace(); --g )
418 if ( _state & VC_STATE_BEGIN) {
419 if ( !tqstricmp( (*j).latin1(), VCARD_BEGIN ) ) {
420 _state = VC_STATE_BODY;
423 _err = VC_ERR_NO_BEGIN;
426 }
else if ( _state & VC_STATE_BODY ) {
427 if ( !tqstricmp( (*j).latin1(), VCARD_END ) ) {
428 _state |= VC_STATE_END;
433 int colon = (*j).find(
':' );
435 _err = VC_ERR_INVALID_LINE;
439 TQString
key = (*j).left( colon );
440 TQString value = (*j).mid( colon + 1 );
444 TQStringList keyTokens = TQStringList::split(
';', key );
445 bool qp =
false, first_pass =
true;
448 if ( keyTokens.count() > 0 ) {
449 _vcl.qualified =
false;
450 _vcl.name = keyTokens[ 0 ].lower();
452 for ( TQStringList::Iterator z = keyTokens.begin(); z != keyTokens.end(); ++z ) {
453 TQString zz = (*z).lower();
454 if ( zz == VCARD_QUOTED_PRINTABLE || zz == VCARD_ENCODING_QUOTED_PRINTABLE ) {
456 }
else if ( zz == VCARD_BASE64 ) {
458 }
else if ( !first_pass ) {
459 _vcl.qualified =
true;
460 _vcl.qualifiers.append( zz );
465 _err = VC_ERR_INVALID_LINE;
471 if ( _vcl.name == VCARD_VERSION )
472 _state |= VC_STATE_HAVE_VERSION;
474 if ( _vcl.name == VCARD_N || _vcl.name == VCARD_FN )
475 _state |= VC_STATE_HAVE_N;
481 if ( value[ value.length() - 1 ] !=
'=' )
484 }
while ( (*j)[ (*j).length() - 1 ] !=
'=' );
487 while ( value[ value.length() - 1 ] ==
'=' ) {
488 value.remove( value.length() - 1, 1 );
489 value.append(*( ++j ));
492 _vcl.parameters = TQStringList::split(
';', value,
true );
494 for ( TQStringList::Iterator z = _vcl.parameters.begin(); z != _vcl.parameters.end(); ++z )
499 _err = VC_ERR_INTERNAL;
504 if ( !_vcl.isValid() ) {
505 _err = VC_ERR_INVALID_LINE;
510 vcdata->append( _vcl );
515 if ( !( _state & VC_STATE_END ) )
516 _err = VC_ERR_NO_END;
518 if ( !( _state & VC_STATE_HAVE_N ) ||
519 !( _state & VC_STATE_HAVE_VERSION ) )
520 _err = VC_ERR_MISSING_MANDATORY;
536 VCard21ParserImpl::VCard21ParserImpl()
541 VCard21ParserImpl::VCard21ParserImpl(TQValueList<VCardLineX> *_vcd)
546 VCard21ParserImpl::~VCard21ParserImpl()
552 TQString VCard21ParserImpl::getValue(
const TQString& name,
const TQString& qualifier)
555 const TQString lowname =
name.lower();
556 const TQString lowqualifier = qualifier.lower();
558 for (TQValueListIterator<VCardLineX> i = _vcdata->begin();i != _vcdata->end();++i) {
559 if ((*i).name == lowname && (*i).qualified && (*i).qualifiers.contains(lowqualifier)) {
560 if ((*i).parameters.count() > 0)
561 return (*i).parameters[0];
569 TQString VCard21ParserImpl::getValue(
const TQString& name)
572 const TQString lowname =
name.lower();
574 for (TQValueListIterator<VCardLineX> i = _vcdata->begin();i != _vcdata->end();++i) {
575 if ((*i).name == lowname && !(*i).qualified) {
576 if ((*i).parameters.count() > 0)
577 return (*i).parameters[0];
585 TQStringList VCard21ParserImpl::getValues(
const TQString& name)
587 const TQString lowname =
name.lower();
588 for (TQValueListIterator<VCardLineX> i = _vcdata->begin();i != _vcdata->end();++i) {
589 if ((*i).name == lowname && !(*i).qualified)
590 return (*i).parameters;
593 return TQStringList();
596 TQStringList VCard21ParserImpl::getValues(
const TQString& name,
const TQString& qualifier)
598 const TQString lowname =
name.lower();
599 const TQString lowqualifier = qualifier.lower();
600 for (TQValueListIterator<VCardLineX> i = _vcdata->begin();i != _vcdata->end();++i) {
601 if ((*i).name == lowname && (*i).qualified && (*i).qualifiers.contains(lowqualifier))
602 return (*i).parameters;
605 return TQStringList();
static TQCString quotedPrintableDecode(const TQByteArray &in)
void insertAddressee(const Addressee &addr)
Insert an addressee into the address book.
Postal address information.
void setPostalCode(const TQString &)
Sets the postal code.
void setExtended(const TQString &)
Sets the extended address information.
void setLocality(const TQString &)
Sets the locality, e.g.
void setType(int type)
Sets the type of address.
void setRegion(const TQString &)
Sets the region, e.g.
void setLabel(const TQString &)
Sets the delivery label.
void setStreet(const TQString &)
Sets the street (including number).
void setCountry(const TQString &)
Sets the country.
void setPostOfficeBox(const TQString &)
Sets the post office box.
void insertPhoneNumber(const PhoneNumber &phoneNumber)
Insert a phone number.
void insertEmail(const TQString &email, bool preferred=false)
Insert an email address.
void insertAddress(const Address &address)
Insert an address.
virtual ~VCard21Parser()
Destructor.
void readFromString(TDEABC::AddressBook *ab, const TQString &str)
Parses a string in vcard2.1 format and saves the single addressees to the address book.
VCard21Parser()
Constructor.
static TDEABC::Address readAddressFromQStringList(const TQStringList &data, const int type)
Helper method to store a address.
kndbgstream & endl(kndbgstream &s)
kdbgstream kdDebug(int area=0)
static data, shared by ALL addressee objects
KABC_EXPORT TQDateTime VCardStringToDate(const TQString &dateString)
Converts a date string as it is used in VCard and LDIF files to a TQDateTime value.
TQString name(StdAccel id)