vcardparser.cpp

00001 /*
00002     This file is part of libkabc.
00003     Copyright (c) 2003 Tobias Koenig <tokoe@kde.org>
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to
00017     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018     Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include <qregexp.h>
00022 
00023 #include <kmdcodec.h>
00024 
00025 #include "vcardparser.h"
00026 
00027 #define FOLD_WIDTH 75
00028 
00029 using namespace KABC;
00030 
00031 static QString backslash( "\\\\" );
00032 static QString comma( "\\," );
00033 static QString newline( "\\n" );
00034 
00035 static void addEscapes( QString &str )
00036 {
00037   str.replace( '\\', backslash );
00038   str.replace( ',', comma );
00039   str.replace( '\n', newline );
00040 }
00041 
00042 static void removeEscapes( QString &str )
00043 {
00044   str.replace( newline, "\n" );
00045   str.replace( comma, "," );
00046   str.replace( backslash, "\\" );
00047 }
00048 
00049 VCardParser::VCardParser()
00050 {
00051 }
00052 
00053 VCardParser::~VCardParser()
00054 {
00055 }
00056 
00057 VCard::List VCardParser::parseVCards( const QString& text )
00058 {
00059   static QRegExp sep( "[\x0d\x0a]" );
00060 
00061   VCard currentVCard;
00062   VCard::List vCardList;
00063   QString currentLine;
00064 
00065   const QStringList lines = QStringList::split( sep, text );
00066   QStringList::ConstIterator it;
00067 
00068   bool inVCard = false;
00069   QStringList::ConstIterator linesEnd( lines.end() );
00070   for ( it = lines.begin(); it != linesEnd; ++it ) {
00071 
00072     if ( (*it).isEmpty() ) // empty line
00073       continue;
00074 
00075     if ( (*it)[ 0 ] == ' ' || (*it)[ 0 ] == '\t' ) { // folded line => append to previous
00076       currentLine += QString( *it ).remove( 0, 1 );
00077       continue;
00078     } else {
00079       if ( inVCard && !currentLine.isEmpty() ) { // now parse the line
00080         int colon = currentLine.find( ':' );
00081         if ( colon == -1 ) { // invalid line
00082           currentLine = (*it);
00083           continue;
00084         }
00085 
00086         VCardLine vCardLine;
00087         const QString key = currentLine.left( colon ).stripWhiteSpace();
00088         QString value = currentLine.mid( colon + 1 );
00089 
00090         QStringList params = QStringList::split( ';', key );
00091 
00092         // check for group
00093         if ( params[0].find( '.' ) != -1 ) {
00094           const QStringList groupList = QStringList::split( '.', params[0] );
00095           vCardLine.setGroup( groupList[0] );
00096           vCardLine.setIdentifier( groupList[1] );
00097         } else
00098           vCardLine.setIdentifier( params[0] );
00099 
00100         if ( params.count() > 1 ) { // find all parameters
00101           QStringList::ConstIterator paramIt = params.begin();
00102           for ( ++paramIt; paramIt != params.end(); ++paramIt ) {
00103             QStringList pair = QStringList::split( '=', *paramIt );
00104             if ( pair.size() == 1 ) {
00105               // correct the fucking 2.1 'standard'
00106               if ( pair[0].lower() == "quoted-printable" ) {
00107                 pair[0] = "encoding";
00108                 pair[1] = "quoted-printable";
00109               } else if ( pair[0].lower() == "base64" ) {
00110                 pair[0] = "encoding";
00111                 pair[1] = "base64";
00112               } else {
00113                 pair.prepend( "type" );
00114               }
00115             }
00116             // This is pretty much a faster pair[1].contains( ',' )...
00117             if ( pair[1].find( ',' ) != -1 ) { // parameter in type=x,y,z format
00118               const QStringList args = QStringList::split( ',', pair[ 1 ] );
00119               QStringList::ConstIterator argIt;
00120               for ( argIt = args.begin(); argIt != args.end(); ++argIt )
00121                 vCardLine.addParameter( pair[0].lower(), *argIt );
00122             } else
00123               vCardLine.addParameter( pair[0].lower(), pair[1] );
00124           }
00125         }
00126 
00127         removeEscapes( value );
00128 
00129         params = vCardLine.parameterList();
00130         if ( params.findIndex( "encoding" ) != -1 ) { // have to decode the data
00131           QByteArray input, output;
00132           input = value.local8Bit();
00133           if ( vCardLine.parameter( "encoding" ).lower() == "b" ||
00134                vCardLine.parameter( "encoding" ).lower() == "base64" )
00135             KCodecs::base64Decode( input, output );
00136           else if ( vCardLine.parameter( "encoding" ).lower() == "quoted-printable" ) {
00137             // join any qp-folded lines
00138             while ( value.at( value.length() - 1 ) == '=' && it != linesEnd ) {
00139               value = value.remove( value.length() - 1, 1 ) + (*it);
00140               ++it;
00141             }
00142             input = value.local8Bit();
00143             KCodecs::quotedPrintableDecode( input, output );
00144           }
00145           if ( vCardLine.parameter( "charset" ).lower() == "utf-8" ) {
00146             vCardLine.setValue( QString::fromUtf8( output.data(), output.size() ) );
00147           } else {
00148             vCardLine.setValue( output );
00149           }
00150         } else if ( vCardLine.parameter( "charset" ).lower() == "utf-8" ) {
00151           vCardLine.setValue( QString::fromUtf8( value.ascii() ) );
00152         } else
00153           vCardLine.setValue( value );
00154 
00155         currentVCard.addLine( vCardLine );
00156       }
00157 
00158       // we do not save the start and end tag as vcardline
00159       if ( (*it).lower().startsWith( "begin:vcard" ) ) {
00160         inVCard = true;
00161         currentLine.setLength( 0 );
00162         currentVCard.clear(); // flush vcard
00163         continue;
00164       }
00165 
00166       if ( (*it).lower().startsWith( "end:vcard" ) ) {
00167         inVCard = false;
00168         vCardList.append( currentVCard );
00169         currentLine.setLength( 0 );
00170         currentVCard.clear(); // flush vcard
00171         continue;
00172       }
00173 
00174       currentLine = (*it);
00175     }
00176   }
00177 
00178   return vCardList;
00179 }
00180 
00181 QString VCardParser::createVCards( const VCard::List& list )
00182 {
00183   QString text;
00184   QString textLine;
00185   QString encodingType;
00186   QStringList idents;
00187   QStringList params;
00188   QStringList values;
00189   QStringList::ConstIterator identIt;
00190   QStringList::Iterator paramIt;
00191   QStringList::ConstIterator valueIt;
00192 
00193   VCardLine::List lines;
00194   VCardLine::List::ConstIterator lineIt;
00195   VCard::List::ConstIterator cardIt;
00196 
00197   bool hasEncoding;
00198 
00199   text.reserve( list.size() * 300 ); // reserve memory to be more efficient
00200 
00201   // iterate over the cards
00202   VCard::List::ConstIterator listEnd( list.end() );
00203   for ( cardIt = list.begin(); cardIt != listEnd; ++cardIt ) {
00204     text.append( "BEGIN:VCARD\r\n" );
00205 
00206     idents = (*cardIt).identifiers();
00207     for ( identIt = idents.constBegin(); identIt != idents.constEnd(); ++identIt ) {
00208       lines = (*cardIt).lines( (*identIt) );
00209 
00210       // iterate over the lines
00211       for ( lineIt = lines.constBegin(); lineIt != lines.constEnd(); ++lineIt ) {
00212         if ( !(*lineIt).value().asString().isEmpty() ) {
00213           if ( (*lineIt).hasGroup() )
00214             textLine = (*lineIt).group() + "." + (*lineIt).identifier();
00215           else
00216             textLine = (*lineIt).identifier();
00217 
00218           params = (*lineIt).parameterList();
00219           hasEncoding = false;
00220           if ( params.count() > 0 ) { // we have parameters
00221             for ( paramIt = params.begin(); paramIt != params.end(); ++paramIt ) {
00222               if ( (*paramIt) == "encoding" ) {
00223                 hasEncoding = true;
00224                 encodingType = (*lineIt).parameter( "encoding" ).lower();
00225               }
00226 
00227               values = (*lineIt).parameters( *paramIt );
00228               for ( valueIt = values.constBegin(); valueIt != values.constEnd(); ++valueIt ) {
00229                 textLine.append( ";" + (*paramIt).upper() );
00230                 if ( !(*valueIt).isEmpty() )
00231                   textLine.append( "=" + (*valueIt) );
00232               }
00233             }
00234           }
00235 
00236           if ( hasEncoding ) { // have to encode the data
00237             QByteArray input, output;
00238             if ( encodingType == "b" ) {
00239               input = (*lineIt).value().toByteArray();
00240               KCodecs::base64Encode( input, output );
00241             } else if ( encodingType == "quoted-printable" ) {
00242               input = (*lineIt).value().toString().utf8();
00243               input.resize( input.size() - 1 ); // strip \0
00244               KCodecs::quotedPrintableEncode( input, output, false );
00245             }
00246 
00247             QString value( output );
00248             addEscapes( value );
00249             textLine.append( ":" + value );
00250           } else {
00251             QString value( (*lineIt).value().asString() );
00252             addEscapes( value );
00253             textLine.append( ":" + value );
00254           }
00255 
00256           if ( textLine.length() > FOLD_WIDTH ) { // we have to fold the line
00257             for ( uint i = 0; i <= ( textLine.length() / FOLD_WIDTH ); ++i )
00258               text.append( ( i == 0 ? "" : " " ) + textLine.mid( i * FOLD_WIDTH, FOLD_WIDTH ) + "\r\n" );
00259           } else
00260             text.append( textLine + "\r\n" );
00261         }
00262       }
00263     }
00264 
00265     text.append( "END:VCARD\r\n" );
00266     text.append( "\r\n" );
00267   }
00268 
00269   return text;
00270 }
KDE Home | KDE Accessibility Home | Description of Access Keys