Diffstat (limited to 'kabc/vcardparser/vcardparser.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | kabc/vcardparser/vcardparser.cpp | 131 |
1 files changed, 69 insertions, 62 deletions
diff --git a/kabc/vcardparser/vcardparser.cpp b/kabc/vcardparser/vcardparser.cpp index bec2a0c..7fae011 100644 --- a/kabc/vcardparser/vcardparser.cpp +++ b/kabc/vcardparser/vcardparser.cpp @@ -39,13 +39,16 @@ 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::Iterator it; + const QStringList lines = QStringList::split( sep, text ); + QStringList::ConstIterator it; bool inVCard = false; - for ( it = lines.begin(); it != lines.end(); ++it ) { + QStringList::ConstIterator linesEnd( lines.end() ); + for ( it = lines.begin(); it != linesEnd; ++it ) { if ( (*it).isEmpty() ) // empty line @@ -53,5 +56,5 @@ VCard::List VCardParser::parseVCards( const QString& text ) if ( (*it)[ 0 ] == ' ' || (*it)[ 0 ] == '\t' ) { // folded line => append to previous - currentLine += (*it).remove( 0, 1 ); + currentLine += QString( *it ).remove( 0, 1 ); continue; } else { @@ -64,20 +67,39 @@ VCard::List VCardParser::parseVCards( const QString& text ) 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] ); -//US if ( pair.size() == 1 ) { + QStringList::ConstIterator paramIt = params.begin(); + for ( ++paramIt; paramIt != params.end(); ++paramIt ) { + QStringList pair = QStringList::split( '=', *paramIt ); if ( pair.count() == 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" ); + } } - 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] ); + // This is pretty much a faster pair[1].contains( ',' )... + if ( pair[1].find( ',' ) != -1 ) { // parameter in type=x,y,z format + const QStringList args = QStringList::split( ',', pair[ 1 ] ); + QStringList::ConstIterator argIt; + for ( argIt = args.begin(); argIt != args.end(); ++argIt ) + vCardLine.addParameter( pair[0].lower(), *argIt ); } else vCardLine.addParameter( pair[0].lower(), pair[1] ); @@ -86,42 +108,36 @@ VCard::List VCardParser::parseVCards( const QString& text ) params = vCardLine.parameterList(); - if ( params.contains( "encoding" ) ) { // have to decode the data -#if 0 + if ( params.findIndex( "encoding" ) != -1 ) { // have to decode the data QByteArray input, output; - input = value.local8Bit(); - if ( vCardLine.parameter( "encoding" ).lower() == "b" ) + if ( vCardLine.parameter( "encoding" ).lower() == "b" || + vCardLine.parameter( "encoding" ).lower() == "base64" ) { + input = value.local8Bit(); KCodecs::base64Decode( input, output ); - else if ( vCardLine.parameter( "encoding" ).lower() == "quoted-printable" ) + } else if ( vCardLine.parameter( "encoding" ).lower() == "quoted-printable" ) { + // join any qp-folded lines + while ( value.mid(value.length()-1,1) == "=" && it != linesEnd ) { + value = value.remove( value.length()-1, 1 ) + (*it); + ++it; + } + input = value.local8Bit(); 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" ) ); + } +//PP our vcards are *supposed* to be in UTF-8 +// if ( vCardLine.parameter( "charset" ).lower() == "utf-8" ) { +// vCardLine.setValue( QString::fromUtf8( output.data(), output.size() ) ); +// } else + vCardLine.setValue( output ); +//PP our vcards are *supposed* to be in UTF-8 +// } else if ( vCardLine.parameter( "charset" ).lower() == "utf-8" ) { +// vCardLine.setValue( QString::fromUtf8( value.ascii() ) ); + } else 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 @@ -132,6 +148,4 @@ VCard::List VCardParser::parseVCards( const QString& text ) inVCard = false; vCardList.append( currentVCard ); - //qDebug("VCardParser::parseVCards has to be verified"); -//US currentLine.setLength( 0 ); currentLine = ""; currentVCard.clear(); // flush vcard @@ -156,26 +170,28 @@ QString VCardParser::createVCards( const VCard::List& list ) QStringList::ConstIterator identIt; QStringList::Iterator paramIt; - QStringList::Iterator valueIt; + QStringList::ConstIterator valueIt; VCardLine::List lines; - VCardLine::List::Iterator lineIt; + VCardLine::List::ConstIterator lineIt; VCard::List::ConstIterator cardIt; bool hasEncoding; - // iterate over the cards - for ( cardIt = list.begin(); cardIt != list.end(); ++cardIt ) { + VCard::List::ConstIterator listEnd( list.end() ); + for ( cardIt = list.begin(); cardIt != listEnd; ++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) ); + 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(); @@ -199,12 +215,5 @@ QString VCardParser::createVCards( const VCard::List& list ) 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; - + input = (*lineIt).valueBytes(); if ( encodingType == "b" ) KCodecs::base64Encode( input, output ); @@ -212,9 +221,7 @@ QString VCardParser::createVCards( const VCard::List& list ) 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" ) ); + } else 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 ) |