You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
303 lines
6.8 KiB
303 lines
6.8 KiB
15 years ago
|
/*
|
||
|
libvcard - vCard parsing library for vCard version 3.0
|
||
|
|
||
|
Copyright (C) 1999 Rik Hemsley rik@kde.org
|
||
|
|
||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||
|
of this software and associated documentation files (the "Software"), to
|
||
|
deal in the Software without restriction, including without limitation the
|
||
|
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||
|
sell copies of the Software, and to permit persons to whom the Software is
|
||
|
furnished to do so, subject to the following conditions:
|
||
|
|
||
|
The above copyright notice and this permission notice shall be included in
|
||
|
all copies or substantial portions of the Software.
|
||
|
|
||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||
|
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||
|
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||
|
*/
|
||
|
|
||
14 years ago
|
#include <tqcstring.h>
|
||
|
#include <tqstrlist.h>
|
||
|
#include <tqregexp.h>
|
||
15 years ago
|
|
||
|
#include <kdebug.h>
|
||
|
|
||
|
#include <VCardAdrParam.h>
|
||
|
#include <VCardAgentParam.h>
|
||
|
#include <VCardDateParam.h>
|
||
|
#include <VCardEmailParam.h>
|
||
|
#include <VCardImageParam.h>
|
||
|
#include <VCardSourceParam.h>
|
||
|
#include <VCardTelParam.h>
|
||
|
#include <VCardTextBinParam.h>
|
||
|
#include <VCardTextParam.h>
|
||
|
|
||
|
#include <VCardAdrValue.h>
|
||
|
#include <VCardAgentValue.h>
|
||
|
#include <VCardDateValue.h>
|
||
|
#include <VCardImageValue.h>
|
||
|
#include <VCardTextValue.h>
|
||
|
#include <VCardTextBinValue.h>
|
||
|
#include <VCardLangValue.h>
|
||
|
#include <VCardNValue.h>
|
||
|
#include <VCardURIValue.h>
|
||
|
#include <VCardSoundValue.h>
|
||
|
#include <VCardClassValue.h>
|
||
|
#include <VCardFloatValue.h>
|
||
|
#include <VCardOrgValue.h>
|
||
|
#include <VCardTelValue.h>
|
||
|
#include <VCardTextListValue.h>
|
||
|
#include <VCardUTCValue.h>
|
||
|
#include <VCardGeoValue.h>
|
||
|
|
||
|
#include <VCardRToken.h>
|
||
|
#include <VCardContentLine.h>
|
||
|
|
||
|
#include <VCardEntity.h>
|
||
|
#include <VCardEnum.h>
|
||
|
#include <VCardDefines.h>
|
||
|
|
||
|
using namespace VCARD;
|
||
|
|
||
|
ContentLine::ContentLine()
|
||
|
: Entity(),
|
||
|
value_(0),
|
||
|
paramType_( ParamUnknown ),
|
||
|
valueType_( ValueUnknown ),
|
||
|
entityType_( EntityUnknown )
|
||
|
{
|
||
|
}
|
||
|
|
||
|
ContentLine::ContentLine(const ContentLine & x)
|
||
|
: Entity(x),
|
||
|
group_ (x.group_),
|
||
|
name_ (x.name_),
|
||
|
paramList_(x.paramList_),
|
||
|
value_(x.value_->clone()),
|
||
|
paramType_( x.paramType_ ),
|
||
|
valueType_( x.valueType_ ),
|
||
|
entityType_( x.entityType_ )
|
||
|
{
|
||
|
}
|
||
|
|
||
14 years ago
|
ContentLine::ContentLine(const TQCString & s)
|
||
15 years ago
|
: Entity(s),
|
||
|
value_(0),
|
||
|
paramType_( ParamUnknown ),
|
||
|
valueType_( ValueUnknown ),
|
||
|
entityType_( EntityUnknown )
|
||
|
{
|
||
|
}
|
||
|
|
||
|
ContentLine &
|
||
|
ContentLine::operator = (ContentLine & x)
|
||
|
{
|
||
|
if (*this == x) return *this;
|
||
|
|
||
|
paramList_ = x.paramList();
|
||
|
value_ = x.value_->clone();
|
||
|
|
||
|
Entity::operator = (x);
|
||
|
return *this;
|
||
|
}
|
||
|
|
||
|
ContentLine &
|
||
14 years ago
|
ContentLine::operator = (const TQCString & s)
|
||
15 years ago
|
{
|
||
|
Entity::operator = (s);
|
||
|
delete value_;
|
||
|
value_ = 0;
|
||
|
return *this;
|
||
|
}
|
||
|
|
||
|
bool
|
||
|
ContentLine::operator == (ContentLine & x)
|
||
|
{
|
||
|
x.parse();
|
||
|
|
||
14 years ago
|
TQPtrListIterator<Param> it(x.paramList());
|
||
15 years ago
|
|
||
13 years ago
|
if (!paramList_.find(it.current()))
|
||
15 years ago
|
return false;
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
ContentLine::~ContentLine()
|
||
|
{
|
||
|
delete value_;
|
||
|
value_ = 0;
|
||
|
}
|
||
|
|
||
|
void
|
||
|
ContentLine::_parse()
|
||
|
{
|
||
|
vDebug("parse");
|
||
|
|
||
|
// Unqote newlines
|
||
13 years ago
|
strRep_ = strRep_.replace( TQRegExp( "\\\\n" ), "\n" );
|
||
15 years ago
|
|
||
13 years ago
|
int split = strRep_.find(':');
|
||
15 years ago
|
|
||
|
if (split == -1) { // invalid content line
|
||
|
vDebug("No ':'");
|
||
|
return;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQCString firstPart(strRep_.left(split));
|
||
|
TQCString valuePart(strRep_.mid(split + 1));
|
||
15 years ago
|
|
||
13 years ago
|
split = firstPart.find('.');
|
||
15 years ago
|
|
||
|
if (split != -1) {
|
||
|
group_ = firstPart.left(split);
|
||
|
firstPart = firstPart.mid(split + 1);
|
||
|
}
|
||
|
|
||
|
vDebug("Group == " + group_);
|
||
|
vDebug("firstPart == " + firstPart);
|
||
|
vDebug("valuePart == " + valuePart);
|
||
|
|
||
|
// Now we have the group, the name and param list together and the value.
|
||
|
|
||
14 years ago
|
TQStrList l;
|
||
15 years ago
|
|
||
|
RTokenise(firstPart, ";", l);
|
||
|
|
||
|
if (l.count() == 0) {// invalid - no name !
|
||
|
vDebug("No name for this content line !");
|
||
|
return;
|
||
|
}
|
||
|
|
||
13 years ago
|
name_ = l.at(0);
|
||
15 years ago
|
|
||
|
// Now we have the name, so the rest of 'l' is the params.
|
||
|
// Remove the name part.
|
||
|
l.remove(0u);
|
||
|
|
||
|
entityType_ = EntityNameToEntityType(name_);
|
||
|
paramType_ = EntityTypeToParamType(entityType_);
|
||
|
|
||
|
unsigned int i = 0;
|
||
|
|
||
|
// For each parameter, create a new parameter of the correct type.
|
||
|
|
||
14 years ago
|
TQStrListIterator it(l);
|
||
15 years ago
|
|
||
|
for (; it.current(); ++it, i++) {
|
||
|
|
||
14 years ago
|
TQCString str = *it;
|
||
15 years ago
|
|
||
13 years ago
|
split = str.find("=");
|
||
15 years ago
|
if (split < 0 ) {
|
||
|
vDebug("No '=' in parameter.");
|
||
|
continue;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQCString paraName = str.left(split);
|
||
|
TQCString paraValue = str.mid(split + 1);
|
||
15 years ago
|
|
||
14 years ago
|
TQStrList paraValues;
|
||
15 years ago
|
RTokenise(paraValue, ",", paraValues);
|
||
|
|
||
14 years ago
|
TQStrListIterator it2( paraValues );
|
||
15 years ago
|
|
||
|
for(; it2.current(); ++it2) {
|
||
|
|
||
|
Param *p = new Param;
|
||
|
p->setName( paraName );
|
||
|
p->setValue( *it2 );
|
||
|
|
||
|
paramList_.append(p);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Create a new value of the correct type.
|
||
|
|
||
|
valueType_ = EntityTypeToValueType(entityType_);
|
||
|
|
||
|
// kdDebug(5710) << "valueType: " << valueType_ << endl;
|
||
|
|
||
|
switch (valueType_) {
|
||
|
|
||
|
case ValueSound: value_ = new SoundValue; break;
|
||
|
case ValueAgent: value_ = new AgentValue; break;
|
||
|
case ValueAddress: value_ = new AdrValue; break;
|
||
|
case ValueTel: value_ = new TelValue; break;
|
||
|
case ValueTextBin: value_ = new TextBinValue; break;
|
||
|
case ValueOrg: value_ = new OrgValue; break;
|
||
|
case ValueN: value_ = new NValue; break;
|
||
|
case ValueUTC: value_ = new UTCValue; break;
|
||
|
case ValueURI: value_ = new URIValue; break;
|
||
|
case ValueClass: value_ = new ClassValue; break;
|
||
|
case ValueFloat: value_ = new FloatValue; break;
|
||
|
case ValueImage: value_ = new ImageValue; break;
|
||
|
case ValueDate: value_ = new DateValue; break;
|
||
|
case ValueTextList: value_ = new TextListValue; break;
|
||
|
case ValueGeo: value_ = new GeoValue; break;
|
||
|
case ValueText:
|
||
|
case ValueUnknown:
|
||
|
default: value_ = new TextValue; break;
|
||
|
}
|
||
|
|
||
|
*value_ = valuePart;
|
||
|
}
|
||
|
|
||
|
void
|
||
|
ContentLine::_assemble()
|
||
|
{
|
||
|
vDebug("Assemble (argl) - my name is \"" + name_ + "\"");
|
||
|
strRep_.truncate(0);
|
||
|
|
||
14 years ago
|
TQCString line;
|
||
15 years ago
|
|
||
|
if (!group_.isEmpty())
|
||
|
line += group_ + '.';
|
||
|
|
||
|
line += name_;
|
||
|
|
||
|
vDebug("Adding parameters");
|
||
|
ParamListIterator it(paramList_);
|
||
|
|
||
|
for (; it.current(); ++it)
|
||
|
line += ";" + it.current()->asString();
|
||
|
|
||
|
vDebug("Adding value");
|
||
|
if (value_ != 0)
|
||
|
line += ":" + value_->asString();
|
||
|
else {
|
||
|
vDebug("No value");
|
||
|
}
|
||
|
|
||
|
// Quote newlines
|
||
13 years ago
|
line = line.replace( TQRegExp( "\n" ), "\\n" );
|
||
15 years ago
|
|
||
|
// Fold lines longer than 72 chars
|
||
|
const int maxLen = 72;
|
||
|
uint cursor = 0;
|
||
|
while( line.length() > ( cursor + 1 ) * maxLen ) {
|
||
|
strRep_ += line.mid( cursor * maxLen, maxLen );
|
||
|
strRep_ += "\r\n ";
|
||
|
++cursor;
|
||
|
}
|
||
|
strRep_ += line.mid( cursor * maxLen );
|
||
|
}
|
||
|
|
||
|
void
|
||
|
ContentLine::clear()
|
||
|
{
|
||
|
group_.truncate(0);
|
||
|
name_.truncate(0);
|
||
|
paramList_.clear();
|
||
|
delete value_;
|
||
|
value_ = 0;
|
||
|
paramType_ = ParamUnknown;
|
||
|
valueType_ = ValueUnknown;
|
||
|
entityType_ = EntityUnknown;
|
||
|
}
|