diff --git a/kabc/vcardparser/vcardline.cpp b/kabc/vcardparser/vcardline.cpp index 046b1842c..6b2284f8d 100644 --- a/kabc/vcardparser/vcardline.cpp +++ b/kabc/vcardparser/vcardline.cpp @@ -1,115 +1,150 @@ /* This file is part of libkabc. Copyright (c) 2003 Tobias Koenig This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "vcardline.h" using namespace KABC; +class VCardLine::VCardLinePrivate +{ + public: + QString mGroup; +}; + VCardLine::VCardLine() + : d( 0 ) { } VCardLine::VCardLine( const QString &identifier ) + : d( 0 ) { mIdentifier = identifier; } VCardLine::VCardLine( const QString &identifier, const QVariant &value ) + : d( 0 ) { mIdentifier = identifier; mValue = value; } VCardLine::VCardLine( const VCardLine& line ) + : d( 0 ) { mParamMap = line.mParamMap; mValue = line.mValue; mIdentifier = line.mIdentifier; } VCardLine::~VCardLine() { + delete d; } VCardLine& VCardLine::operator=( const VCardLine& line ) { if ( &line == this ) return *this; mParamMap = line.mParamMap; mValue = line.mValue; mIdentifier = line.mIdentifier; return *this; } void VCardLine::setIdentifier( const QString& identifier ) { mIdentifier = identifier; } QString VCardLine::identifier() const { return mIdentifier; } void VCardLine::setValue( const QVariant& value ) { mValue = value; } QVariant VCardLine::value() const { return mValue; } +void VCardLine::setGroup( const QString& group ) +{ + if ( !d ) + d = new VCardLinePrivate(); + + d->mGroup = group; +} + +QString VCardLine::group() const +{ + if ( d ) + return d->mGroup; + else + return QString(); +} + +bool VCardLine::hasGroup() const +{ + if ( !d ) + return false; + else + return d->mGroup.isEmpty(); +} + QStringList VCardLine::parameterList() const { return mParamMap.keys(); } void VCardLine::addParameter( const QString& param, const QString& value ) { QStringList &list = mParamMap[ param ]; if ( list.findIndex( value ) == -1 ) // not included yet list.append( value ); } QStringList VCardLine::parameters( const QString& param ) const { ParamMap::ConstIterator it = mParamMap.find( param ); if ( it == mParamMap.end() ) return QStringList(); else return *it; } QString VCardLine::parameter( const QString& param ) const { ParamMap::ConstIterator it = mParamMap.find( param ); if ( it == mParamMap.end() ) return QString::null; else { if ( (*it).isEmpty() ) return QString::null; else return (*it).first(); } } diff --git a/kabc/vcardparser/vcardline.h b/kabc/vcardparser/vcardline.h index dd7f54a6e..645b5ff6b 100644 --- a/kabc/vcardparser/vcardline.h +++ b/kabc/vcardparser/vcardline.h @@ -1,100 +1,115 @@ /* This file is part of libkabc. Copyright (c) 2003 Tobias Koenig This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifndef VCARDLINE_H #define VCARDLINE_H #include #include #include #include #include namespace KABC { class VCardLine { public: typedef QValueList List; typedef QMap ParamMap; VCardLine(); VCardLine( const QString &identifier ); VCardLine( const QString &identifier, const QVariant &value ); VCardLine( const VCardLine& ); ~VCardLine(); VCardLine& operator=( const VCardLine& ); /** * Sets the identifier of this line e.g. UID, FN, CLASS */ void setIdentifier( const QString& identifier ); /** * Returns the identifier of this line. */ QString identifier() const; /** * Sets the value of of this line. */ void setValue( const QVariant& value ); /** * Returns the value of this line. */ QVariant value() const; + /** + * Sets the group the line belongs to. + */ + void setGroup( const QString& group ); + + /** + * Returns the group the line belongs to. + */ + QString group() const; + + /** + * Returns whether the line belongs to a group. + */ + bool hasGroup() const; + /** * Returns all parameters. */ QStringList parameterList() const; /** * Add a new parameter to the line. */ void addParameter( const QString& param, const QString& value ); /** * Returns the values of a special parameter. * You can get a list of all parameters with paramList(). */ QStringList parameters( const QString& param ) const; /** * Returns only the first value of a special parameter. * You can get a list of all parameters with paramList(). */ QString parameter( const QString& param ) const; private: ParamMap mParamMap; QString mIdentifier; QVariant mValue; class VCardLinePrivate; VCardLinePrivate *d; }; } #endif diff --git a/kabc/vcardparser/vcardparser.cpp b/kabc/vcardparser/vcardparser.cpp index 676c8b9d9..b834d6ade 100644 --- a/kabc/vcardparser/vcardparser.cpp +++ b/kabc/vcardparser/vcardparser.cpp @@ -1,218 +1,231 @@ /* This file is part of libkabc. Copyright (c) 2003 Tobias Koenig This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include "vcardparser.h" #define FOLD_WIDTH 75 using namespace KABC; VCardParser::VCardParser() { } VCardParser::~VCardParser() { } VCard::List VCardParser::parseVCards( const QString& text ) { + static QRegExp sep( "[\x0d\x0a]" ); + VCard currentVCard; VCard::List vCardList; QString currentLine; - QStringList lines = QStringList::split( QRegExp( "[\x0d\x0a]" ), text ); + QStringList lines = QStringList::split( sep, text ); QStringList::Iterator it; bool inVCard = false; for ( it = lines.begin(); it != lines.end(); ++it ) { if ( (*it).isEmpty() ) // empty line continue; if ( (*it)[ 0 ] == ' ' || (*it)[ 0 ] == '\t' ) { // folded line => append to previous currentLine += (*it).remove( 0, 1 ); continue; } else { if ( inVCard && !currentLine.isEmpty() ) { // now parse the line int colon = currentLine.find( ':' ); if ( colon == -1 ) { // invalid line currentLine = (*it); continue; } VCardLine vCardLine; - QString key = currentLine.left( colon ).stripWhiteSpace(); + const QString key = currentLine.left( colon ).stripWhiteSpace(); QString value = currentLine.mid( colon + 1 ); QStringList params = QStringList::split( ';', key ); - vCardLine.setIdentifier( params[0] ); + + // check for group + if ( params[0].find( '.' ) != -1 ) { + const QStringList groupList = QStringList::split( '.', params[0] ); + vCardLine.setGroup( groupList[0] ); + vCardLine.setIdentifier( groupList[1] ); + } else + vCardLine.setIdentifier( params[0] ); + if ( params.count() > 1 ) { // find all parameters for ( uint i = 1; i < params.count(); ++i ) { QStringList pair = QStringList::split( '=', params[i] ); if ( pair.size() == 1 ) { // correct the fucking 2.1 'standard' if ( pair[0].lower() == "quoted-printable" ) { pair[0] = "encoding"; pair[1] = "quoted-printable"; } else if ( pair[0].lower() == "base64" ) { pair[0] = "encoding"; pair[1] = "base64"; } else { pair.prepend( "type" ); } } //This is pretty much a faster pair[1].contains( ',' )... if ( pair[1].find( ',' ) != -1 ) { // parameter in type=x,y,z format - QStringList args = QStringList::split( ',', pair[ 1 ] ); + const QStringList args = QStringList::split( ',', pair[ 1 ] ); for ( uint j = 0; j < args.count(); ++j ) vCardLine.addParameter( pair[0].lower(), args[j] ); } else vCardLine.addParameter( pair[0].lower(), pair[1] ); } } params = vCardLine.parameterList(); if ( params.findIndex( "encoding" ) != -1 ) { // have to decode the data QByteArray input, output; input = value.local8Bit(); if ( vCardLine.parameter( "encoding" ).lower() == "b" || vCardLine.parameter( "encoding" ).lower() == "base64" ) KCodecs::base64Decode( input, output ); else if ( vCardLine.parameter( "encoding" ).lower() == "quoted-printable" ) KCodecs::quotedPrintableDecode( input, output ); if ( vCardLine.parameter( "charset" ).lower() == "utf-8" ) { vCardLine.setValue( QString::fromUtf8( output.data(), output.size() ) ); } else { vCardLine.setValue( output ); } } else if ( vCardLine.parameter( "charset" ).lower() == "utf-8" ) { vCardLine.setValue( QString::fromUtf8( value.ascii() ) ); } else vCardLine.setValue( value.replace( "\\n", "\n" ) ); currentVCard.addLine( vCardLine ); } // we do not save the start and end tag as vcardline if ( (*it).lower().startsWith( "begin:vcard" ) ) { inVCard = true; currentLine.setLength( 0 ); currentVCard.clear(); // flush vcard continue; } if ( (*it).lower().startsWith( "end:vcard" ) ) { inVCard = false; vCardList.append( currentVCard ); currentLine.setLength( 0 ); currentVCard.clear(); // flush vcard continue; } currentLine = (*it); } } return vCardList; } QString VCardParser::createVCards( const VCard::List& list ) { QString text; QString textLine; QString encodingType; QStringList idents; QStringList params; QStringList values; QStringList::ConstIterator identIt; QStringList::Iterator paramIt; QStringList::Iterator valueIt; VCardLine::List lines; VCardLine::List::Iterator lineIt; VCard::List::ConstIterator cardIt; bool hasEncoding; text.reserve( list.size() * 300 ); // reserve memory to be more efficient // iterate over the cards for ( cardIt = list.begin(); cardIt != list.end(); ++cardIt ) { text.append( "BEGIN:VCARD\r\n" ); idents = (*cardIt).identifiers(); for ( identIt = idents.begin(); identIt != idents.end(); ++identIt ) { lines = (*cardIt).lines( (*identIt) ); // iterate over the lines for ( lineIt = lines.begin(); lineIt != lines.end(); ++lineIt ) { if ( !(*lineIt).value().asString().isEmpty() ) { - textLine = (*lineIt).identifier(); + if ( (*lineIt).hasGroup() ) + textLine = (*lineIt).group() + "." + (*lineIt).identifier(); + else + textLine = (*lineIt).identifier(); params = (*lineIt).parameterList(); hasEncoding = false; if ( params.count() > 0 ) { // we have parameters for ( paramIt = params.begin(); paramIt != params.end(); ++paramIt ) { if ( (*paramIt) == "encoding" ) { hasEncoding = true; encodingType = (*lineIt).parameter( "encoding" ).lower(); } values = (*lineIt).parameters( *paramIt ); for ( valueIt = values.begin(); valueIt != values.end(); ++valueIt ) { textLine.append( ";" + (*paramIt).upper() ); if ( !(*valueIt).isEmpty() ) textLine.append( "=" + (*valueIt) ); } } } if ( hasEncoding ) { // have to encode the data QByteArray input, output; input = (*lineIt).value().toByteArray(); if ( encodingType == "b" ) KCodecs::base64Encode( input, output ); else if ( encodingType == "quoted-printable" ) KCodecs::quotedPrintableEncode( input, output ); textLine.append( ":" + QString( output ) ); } else textLine.append( ":" + (*lineIt).value().asString().replace( "\n", "\\n" ) ); if ( textLine.length() > FOLD_WIDTH ) { // we have to fold the line for ( uint i = 0; i <= ( textLine.length() / FOLD_WIDTH ); ++i ) text.append( ( i == 0 ? "" : " " ) + textLine.mid( i * FOLD_WIDTH, FOLD_WIDTH ) + "\r\n" ); } else text.append( textLine + "\r\n" ); } } } text.append( "END:VCARD\r\n" ); text.append( "\r\n" ); } return text; }