-rw-r--r-- | kabc/vcardparser/vcardparser.cpp | 2 | ||||
-rw-r--r-- | kabc/vcardparser/vcardtool.cpp | 10 |
2 files changed, 6 insertions, 6 deletions
diff --git a/kabc/vcardparser/vcardparser.cpp b/kabc/vcardparser/vcardparser.cpp index 9ea084d..bec2a0c 100644 --- a/kabc/vcardparser/vcardparser.cpp +++ b/kabc/vcardparser/vcardparser.cpp @@ -1,233 +1,233 @@ /* This file is part of libkabc. Copyright (c) 2003 Tobias Koenig <tokoe@kde.org> 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 <qregexp.h> #include <kmdcodec.h> #include "vcardparser.h" #define FOLD_WIDTH 75 using namespace KABC; VCardParser::VCardParser() { } VCardParser::~VCardParser() { } VCard::List VCardParser::parseVCards( const QString& text ) { VCard currentVCard; VCard::List vCardList; QString currentLine; QStringList lines = QStringList::split( QRegExp( "[\x0d\x0a]" ), 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(); QString value = currentLine.mid( colon + 1 ); QStringList params = QStringList::split( ';', key ); 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] ); //US if ( pair.size() == 1 ) { if ( pair.count() == 1 ) { pair.prepend( "type" ); } if ( pair[1].contains( ',' ) ) { // parameter in type=x,y,z format 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.contains( "encoding" ) ) { // have to decode the data #if 0 QByteArray input, output; input = value.local8Bit(); if ( vCardLine.parameter( "encoding" ).lower() == "b" ) KCodecs::base64Decode( input, output ); else if ( vCardLine.parameter( "encoding" ).lower() == "quoted-printable" ) KCodecs::quotedPrintableDecode( input, output ); //qDebug("VCardParser::parseVCards has to be verified"); //US I am not sure if this is correct //US vCardLine.setValue( output ); QCString cs(output); qDebug("len1 %d len2 %d ",input.size(), output.size( )); #endif QCString cs = value.local8Bit(); qDebug("****************************************** "); qDebug("************* WARNING ******************** "); qDebug("****************************************** "); qDebug("Make sure, the decoding is done after"); qDebug("QVariant conversion!"); qDebug("Insert Line DECODING OKAY, where this is implemented"); // use for decoding the above code! vCardLine.setValue( cs ); } else { //qDebug("VCardParser::parseVCards has to be verified"); //US vCardLine.setValue( value.replace( "\\n", "\n" ) ); - vCardLine.setValue( value.replace( QRegExp("\\n"), "\n" ) ); + vCardLine.setValue( value.replace( QRegExp("\\\\n"), "\n" ) ); } currentVCard.addLine( vCardLine ); } // we do not save the start and end tag as vcardline if ( (*it).lower().startsWith( "begin:vcard" ) ) { inVCard = true; //qDebug("VCardParser::parseVCards has to be verified"); //US currentLine.setLength( 0 ); currentLine = ""; currentVCard.clear(); // flush vcard continue; } if ( (*it).lower().startsWith( "end:vcard" ) ) { inVCard = false; vCardList.append( currentVCard ); //qDebug("VCardParser::parseVCards has to be verified"); //US currentLine.setLength( 0 ); currentLine = ""; 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; // 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 ) { VCard card = (*cardIt); lines = card.lines( (*identIt) ); // iterate over the lines for ( lineIt = lines.begin(); lineIt != lines.end(); ++lineIt ) { if ( !(*lineIt).value().asString().isEmpty() ) { 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; qDebug("VCardParser::createVCards has to be verified"); //US input = (*lineIt).value().toByteArray(); //US I am not sure if this is correct QCString cs ((*lineIt).value().toCString()); input = cs; if ( encodingType == "b" ) KCodecs::base64Encode( input, output ); else if ( encodingType == "quoted-printable" ) KCodecs::quotedPrintableEncode( input, output ); textLine.append( ":" + QString( output ) ); } else { qDebug("VCardParser::createVCards has to be verified"); //US textLine.append( ":" + (*lineIt).value().asString().replace( "\n", "\\n" ) ); textLine.append( ":" + (*lineIt).value().asString().replace( QRegExp("\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; } diff --git a/kabc/vcardparser/vcardtool.cpp b/kabc/vcardparser/vcardtool.cpp index 3fb212e..d1f823b 100644 --- a/kabc/vcardparser/vcardtool.cpp +++ b/kabc/vcardparser/vcardtool.cpp @@ -431,468 +431,468 @@ Addressee::List VCardTool::parseVCards( const QString& vcard ) // N if ( identifier == "n" ) { QStringList nameParts = splitString( semicolonSep, (*lineIt).value().asString() ); if ( nameParts.count() > 0 ) addr.setFamilyName( nameParts[ 0 ] ); if ( nameParts.count() > 1 ) addr.setGivenName( nameParts[ 1 ] ); if ( nameParts.count() > 2 ) addr.setAdditionalName( nameParts[ 2 ] ); if ( nameParts.count() > 3 ) addr.setPrefix( nameParts[ 3 ] ); if ( nameParts.count() > 4 ) addr.setSuffix( nameParts[ 4 ] ); } // NICKNAME if ( identifier == "nickname" ) addr.setNickName( (*lineIt).value().asString() ); // NOTE if ( identifier == "note" ) { // #ifdef DESKTOP_VERSION // addr.setNote( (*lineIt).value().asString() ); // #else QString note = (*lineIt).value().asString(); if ( ! note.isEmpty() ) addr.setNote( note.replace ( QRegExp("\\\\n") , "\n") ); else addr.setNote( note ); //#endif } // ORGANIZATION if ( identifier == "org" ) addr.setOrganization( (*lineIt).value().asString() ); // PHOTO if ( identifier == "photo" ) addr.setPhoto( parsePicture( *lineIt ) ); // PROID if ( identifier == "prodid" ) addr.setProductId( (*lineIt).value().asString() ); // REV if ( identifier == "rev" ) addr.setRevision( parseDateTime( (*lineIt).value().asString() ) ); // ROLE if ( identifier == "role" ) addr.setRole( (*lineIt).value().asString() ); // SORT-STRING if ( identifier == "sort-string" ) addr.setSortString( (*lineIt).value().asString() ); // SOUND if ( identifier == "sound" ) addr.setSound( parseSound( *lineIt ) ); // TEL if ( identifier == "tel" ) { PhoneNumber phone; phone.setNumber( (*lineIt).value().asString() ); int type = 0; QStringList types = (*lineIt).parameters( "type" ); for ( QStringList::Iterator it = types.begin(); it != types.end(); ++it ) type += mPhoneTypeMap[(*it).upper()]; if ( !type ) type = PhoneNumber::Home; // default phone.setType( type ); addr.insertPhoneNumber( phone ); } // TITLE if ( identifier == "title" ) addr.setTitle( (*lineIt).value().asString() ); // TZ if ( identifier == "tz" ) { TimeZone tz; QString date = (*lineIt).value().asString(); int hours = date.mid( 1, 2).toInt(); int minutes = date.mid( 4, 2 ).toInt(); int offset = ( hours * 60 ) + minutes; offset = offset * ( date[ 0 ] == '+' ? 1 : -1 ); tz.setOffset( offset ); addr.setTimeZone( tz ); } // UID if ( identifier == "uid" ) addr.setUid( (*lineIt).value().asString() ); // URL if ( identifier == "url" ) addr.setUrl( (*lineIt).value().asString() ); // X- if ( identifier.startsWith( "x-" ) ) { QString key = (*lineIt).identifier().mid( 2 ); int dash = key.find( "-" ); addr.insertCustom( key.left( dash ), key.mid( dash + 1 ), (*lineIt).value().asString() ); } } } addrList.append( addr ); } return addrList; } QDateTime VCardTool::parseDateTime( const QString &str ) { QDateTime dateTime; if ( str.find( '-' ) == -1 ) { // is base format (yyyymmdd) dateTime.setDate( QDate( str.left( 4 ).toInt(), str.mid( 4, 2 ).toInt(), str.mid( 6, 2 ).toInt() ) ); if ( str.find( 'T' ) ) // has time information yyyymmddThh:mm:ss dateTime.setTime( QTime( str.mid( 11, 2 ).toInt(), str.mid( 14, 2 ).toInt(), str.mid( 17, 2 ).toInt() ) ); } else { // is extended format yyyy-mm-dd dateTime.setDate( QDate( str.left( 4 ).toInt(), str.mid( 5, 2 ).toInt(), str.mid( 8, 2 ).toInt() ) ); if ( str.find( 'T' ) ) // has time information yyyy-mm-ddThh:mm:ss dateTime.setTime( QTime( str.mid( 11, 2 ).toInt(), str.mid( 14, 2 ).toInt(), str.mid( 17, 2 ).toInt() ) ); } return dateTime; } QString VCardTool::createDateTime( const QDateTime &dateTime ) { QString str; if ( dateTime.date().isValid() ) { str.sprintf( "%4d-%02d-%02d", dateTime.date().year(), dateTime.date().month(), dateTime.date().day() ); if ( dateTime.time().isValid() ) { QString tmp; tmp.sprintf( "T%02d:%02d:%02dZ", dateTime.time().hour(), dateTime.time().minute(), dateTime.time().second() ); str += tmp; } } return str; } Picture VCardTool::parsePicture( const VCardLine &line ) { Picture pic; QStringList params = line.parameterList(); if ( params.contains( "encoding" ) ) { QCString cs(line.value().asCString()); QByteArray input, output; input = line.value().asCString(); if ( line.parameter( "encoding" ).lower() == "b" ) KCodecs::base64Decode( input, output ); else if ( line.parameter( "encoding" ).lower() == "quoted-printable" ) KCodecs::quotedPrintableDecode( input, output ); qDebug("********** DECODING OKAY ************** (picture)"); pic.setData( QImage(output) ); } else if ( params.contains( "value" ) ) { if ( line.parameter( "value" ).lower() == "uri" ) pic.setUrl( line.value().asString() ); } if ( params.contains( "type" ) ) pic.setType( line.parameter( "type" ) ); return pic; } VCardLine VCardTool::createPicture( const QString &identifier, const Picture &pic ) { // LR fixed VCardLine line( identifier ); if ( pic.isIntern() ) { if ( !pic.data().isNull() ) { #if 0 QByteArray input; QDataStream s( input, IO_WriteOnly ); s.setVersion( 4 ); s << pic.data(); line.setValue( input ); #else QCString input; QDataStream s( input, IO_WriteOnly ); s.setVersion( 4 ); s << pic.data(); //QCString cs(line.value().asCString()); //QImage qi(cs); line.setValue( input ); #endif line.addParameter( "encoding", "b" ); line.addParameter( "type", "image/png" ); } } else if ( !pic.url().isEmpty() ) { line.setValue( pic.url() ); line.addParameter( "value", "URI" ); } return line; } Sound VCardTool::parseSound( const VCardLine &line ) { Sound snd; QStringList params = line.parameterList(); if ( params.contains( "encoding" ) ) { qDebug("VCardTool::parseSound has to be verified"); //US snd.setData( line.value().asByteArray() ); //US I am not sure if this is correct QCString cs(line.value().asCString()); snd.setData( cs ); } else if ( params.contains( "value" ) ) { if ( line.parameter( "value" ).lower() == "uri" ) snd.setUrl( line.value().asString() ); } /* TODO: support sound types if ( params.contains( "type" ) ) snd.setType( line.parameter( "type" ) ); */ return snd; } VCardLine VCardTool::createSound( const Sound &snd ) { VCardLine line( "SOUND" ); if ( snd.isIntern() ) { if ( !snd.data().isEmpty() ) { qDebug("VCardTool::createSound has to be verified"); //US line.setValue( snd.data() ); //US I am not sure if this is correct QCString cs(snd.data()); line.setValue( cs ); line.addParameter( "encoding", "b" ); // TODO: need to store sound type!!! } } else if ( !snd.url().isEmpty() ) { line.setValue( snd.url() ); line.addParameter( "value", "URI" ); } return line; } Key VCardTool::parseKey( const VCardLine &line ) { Key key; QStringList params = line.parameterList(); if ( params.contains( "encoding" ) ) { qDebug("VCardTool::parseKey has to be verified"); //US key.setBinaryData( line.value().asByteArray() ); //US I am not sure if this is correct QCString cs( line.value().asCString() ); key.setBinaryData( cs ); } else key.setTextData( line.value().asString() ); if ( params.contains( "type" ) ) { if ( line.parameter( "type" ).lower() == "x509" ) key.setType( Key::X509 ); else if ( line.parameter( "type" ).lower() == "pgp" ) key.setType( Key::PGP ); else { key.setType( Key::Custom ); key.setCustomTypeString( line.parameter( "type" ) ); } } return key; } VCardLine VCardTool::createKey( const Key &key ) { VCardLine line( "KEY" ); if ( key.isBinary() ) { if ( !key.binaryData().isEmpty() ) { qDebug("VCardTool::createKey has to be verified"); //US line.setValue( key.binaryData() ); //US I am not sure if this is correct QCString cs(key.binaryData()); line.setValue( cs ); line.addParameter( "encoding", "b" ); } } else if ( !key.textData().isEmpty() ) line.setValue( key.textData() ); if ( key.type() == Key::X509 ) line.addParameter( "type", "X509" ); else if ( key.type() == Key::PGP ) line.addParameter( "type", "PGP" ); else if ( key.type() == Key::Custom ) line.addParameter( "type", key.customTypeString() ); return line; } Secrecy VCardTool::parseSecrecy( const VCardLine &line ) { Secrecy secrecy; if ( line.value().asString().lower() == "public" ) secrecy.setType( Secrecy::Public ); if ( line.value().asString().lower() == "private" ) secrecy.setType( Secrecy::Private ); if ( line.value().asString().lower() == "confidential" ) secrecy.setType( Secrecy::Confidential ); return secrecy; } VCardLine VCardTool::createSecrecy( const Secrecy &secrecy ) { VCardLine line( "CLASS" ); int type = secrecy.type(); if ( type == Secrecy::Public ) line.setValue( "PUBLIC" ); else if ( type == Secrecy::Private ) line.setValue( "PRIVATE" ); else if ( type == Secrecy::Confidential ) line.setValue( "CONFIDENTIAL" ); return line; } Agent VCardTool::parseAgent( const VCardLine &line ) { Agent agent; QStringList params = line.parameterList(); if ( params.contains( "value" ) ) { if ( line.parameter( "value" ).lower() == "uri" ) agent.setUrl( line.value().asString() ); } else { QString str = line.value().asString(); //US using the old implementation instead qDebug("VCardTool::parseAgent has to be verified"); /*US str.replace( "\\n", "\r\n" ); str.replace( "\\N", "\r\n" ); str.replace( "\\;", ";" ); str.replace( "\\:", ":" ); str.replace( "\\,", "," ); */ - str.replace( QRegExp("\\n") , "\r\n" ); - str.replace( QRegExp("\\N") , "\r\n" ); - str.replace( QRegExp("\\;") , ";" ); - str.replace( QRegExp("\\:") , ":" ); - str.replace( QRegExp("\\,") , "," ); + str.replace( QRegExp("\\\\n") , "\r\n" ); + str.replace( QRegExp("\\\\N") , "\r\n" ); + str.replace( QRegExp("\\\\;") , ";" ); + str.replace( QRegExp("\\\\:") , ":" ); + str.replace( QRegExp("\\\\,") , "," ); Addressee::List list = parseVCards( str ); if ( list.count() > 0 ) { Addressee *addr = new Addressee; *addr = list[ 0 ]; agent.setAddressee( addr ); } } return agent; } VCardLine VCardTool::createAgent( VCard::Version version, const Agent &agent ) { VCardLine line( "AGENT" ); if ( agent.isIntern() ) { if ( agent.addressee() != 0 ) { Addressee::List list; list.append( *agent.addressee() ); QString str = createVCards( list, version ); //US using the old implementation instead qDebug("VCardTool::createAgent has to be verified"); /*US str.replace( "\r\n", "\\n" ); str.replace( ";", "\\;" ); str.replace( ":", "\\:" ); str.replace( ",", "\\," ); */ str.replace( QRegExp("\r\n"), "\\n" ); str.replace( QRegExp(";"), "\\;" ); str.replace( QRegExp(":"), "\\:" ); str.replace( QRegExp(","), "\\," ); line.setValue( str ); } } else if ( !agent.url().isEmpty() ) { line.setValue( agent.url() ); line.addParameter( "value", "URI" ); } return line; } QStringList VCardTool::splitString( const QChar &sep, const QString &str ) { QStringList list; QString value( str ); int start = 0; int pos = value.find( sep, start ); while ( pos != -1 ) { if ( value[ pos - 1 ] != '\\' ) { if ( pos > start && pos <= (int)value.length() ) list << value.mid( start, pos - start ); else list << QString::null; start = pos + 1; pos = value.find( sep, start ); } else { if ( pos != 0 ) { value.replace( pos - 1, 2, sep ); pos = value.find( sep, pos ); } else pos = value.find( sep, pos + 1 ); } } int l = value.length() - 1; if ( value.mid( start, l - start + 1 ).length() > 0 ) list << value.mid( start, l - start + 1 ); else list << QString::null; return list; } |