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 static QString cr( "\\r" );
00035 
00036 static void addEscapes( QString &str )
00037 {
00038   str.replace( '\\', backslash );
00039   str.replace( ',', comma );
00040   str.replace( '\r', cr );
00041   str.replace( '\n', newline );
00042 }
00043 
00044 static void removeEscapes( QString &str )
00045 {
00046   str.replace( cr, "\\r" );
00047   str.replace( newline, "\n" );
00048   str.replace( comma, "," );
00049   str.replace( backslash, "\\" );
00050 }
00051 
00052 VCardParser::VCardParser()
00053 {
00054 }
00055 
00056 VCardParser::~VCardParser()
00057 {
00058 }
00059 
00060 VCard::List VCardParser::parseVCards( const QString& text )
00061 {
00062   static QRegExp sep( "[\x0d\x0a]" );
00063 
00064   VCard currentVCard;
00065   VCard::List vCardList;
00066   QString currentLine;
00067 
00068   const QStringList lines = QStringList::split( sep, text );
00069   QStringList::ConstIterator it;
00070 
00071   bool inVCard = false;
00072   QStringList::ConstIterator linesEnd( lines.end() );
00073   for ( it = lines.begin(); it != linesEnd; ++it ) {
00074 
00075     if ( (*it).isEmpty() ) // empty line
00076       continue;
00077 
00078     if ( (*it)[ 0 ] == ' ' || (*it)[ 0 ] == '\t' ) { // folded line => append to previous
00079       currentLine += QString( *it ).remove( 0, 1 );
00080       continue;
00081     } else {
00082       if ( inVCard && !currentLine.isEmpty() ) { // now parse the line
00083         int colon = currentLine.find( ':' );
00084         if ( colon == -1 ) { // invalid line
00085           currentLine = (*it);
00086           continue;
00087         }
00088 
00089         VCardLine vCardLine;
00090         const QString key = currentLine.left( colon ).stripWhiteSpace();
00091         QString value = currentLine.mid( colon + 1 );
00092 
00093         QStringList params = QStringList::split( ';', key );
00094 
00095         // check for group
00096         if ( params[0].find( '.' ) != -1 ) {
00097           const QStringList groupList = QStringList::split( '.', params[0] );
00098           vCardLine.setGroup( groupList[0] );
00099           vCardLine.setIdentifier( groupList[1] );
00100         } else
00101           vCardLine.setIdentifier( params[0] );
00102 
00103         if ( params.count() > 1 ) { // find all parameters
00104           QStringList::ConstIterator paramIt = params.begin();
00105           for ( ++paramIt; paramIt != params.end(); ++paramIt ) {
00106             QStringList pair = QStringList::split( '=', *paramIt );
00107             if ( pair.size() == 1 ) {
00108               // correct the fucking 2.1 'standard'
00109               if ( pair[0].lower() == "quoted-printable" ) {
00110                 pair[0] = "encoding";
00111                 pair[1] = "quoted-printable";
00112               } else if ( pair[0].lower() == "base64" ) {
00113                 pair[0] = "encoding";
00114                 pair[1] = "base64";
00115               } else {
00116                 pair.prepend( "type" );
00117               }
00118             }
00119             // This is pretty much a faster pair[1].contains( ',' )...
00120             if ( pair[1].find( ',' ) != -1 ) { // parameter in type=x,y,z format
00121               const QStringList args = QStringList::split( ',', pair[ 1 ] );
00122               QStringList::ConstIterator argIt;
00123               for ( argIt = args.begin(); argIt != args.end(); ++argIt )
00124                 vCardLine.addParameter( pair[0].lower(), *argIt );
00125             } else
00126               vCardLine.addParameter( pair[0].lower(), pair[1] );
00127           }
00128         }
00129 
00130         removeEscapes( value );
00131 
00132         params = vCardLine.parameterList();
00133         if ( params.findIndex( "encoding" ) != -1 ) { // have to decode the data
00134           QByteArray input, output;
00135           input = value.local8Bit();
00136           if ( vCardLine.parameter( "encoding" ).lower() == "b" ||
00137                vCardLine.parameter( "encoding" ).lower() == "base64" )
00138             KCodecs::base64Decode( input, output );
00139           else if ( vCardLine.parameter( "encoding" ).lower() == "quoted-printable" ) {
00140             // join any qp-folded lines
00141             while ( value.at( value.length() - 1 ) == '=' && it != linesEnd ) {
00142               value = value.remove( value.length() - 1, 1 ) + (*it);
00143               ++it;
00144             }
00145             input = value.local8Bit();
00146             KCodecs::quotedPrintableDecode( input, output );
00147           }
00148           if ( vCardLine.parameter( "charset" ).lower() == "utf-8" ) {
00149             vCardLine.setValue( QString::fromUtf8( output.data(), output.size() ) );
00150           } else {
00151             vCardLine.setValue( output );
00152           }
00153         } else if ( vCardLine.parameter( "charset" ).lower() == "utf-8" ) {
00154           vCardLine.setValue( QString::fromUtf8( value.ascii() ) );
00155         } else
00156           vCardLine.setValue( value );
00157 
00158         currentVCard.addLine( vCardLine );
00159       }
00160 
00161       // we do not save the start and end tag as vcardline
00162       if ( (*it).lower().startsWith( "begin:vcard" ) ) {
00163         inVCard = true;
00164         currentLine.setLength( 0 );
00165         currentVCard.clear(); // flush vcard
00166         continue;
00167       }
00168 
00169       if ( (*it).lower().startsWith( "end:vcard" ) ) {
00170         inVCard = false;
00171         vCardList.append( currentVCard );
00172         currentLine.setLength( 0 );
00173         currentVCard.clear(); // flush vcard
00174         continue;
00175       }
00176 
00177       currentLine = (*it);
00178     }
00179   }
00180 
00181   return vCardList;
00182 }
00183 
00184 QString VCardParser::createVCards( const VCard::List& list )
00185 {
00186   QString text;
00187   QString textLine;
00188   QString encodingType;
00189   QStringList idents;
00190   QStringList params;
00191   QStringList values;
00192   QStringList::ConstIterator identIt;
00193   QStringList::Iterator paramIt;
00194   QStringList::ConstIterator valueIt;
00195 
00196   VCardLine::List lines;
00197   VCardLine::List::ConstIterator lineIt;
00198   VCard::List::ConstIterator cardIt;
00199 
00200   bool hasEncoding;
00201 
00202   text.reserve( list.size() * 300 ); // reserve memory to be more efficient
00203 
00204   // iterate over the cards
00205   VCard::List::ConstIterator listEnd( list.end() );
00206   for ( cardIt = list.begin(); cardIt != listEnd; ++cardIt ) {
00207     text.append( "BEGIN:VCARD\r\n" );
00208 
00209     idents = (*cardIt).identifiers();
00210     for ( identIt = idents.constBegin(); identIt != idents.constEnd(); ++identIt ) {
00211       lines = (*cardIt).lines( (*identIt) );
00212 
00213       // iterate over the lines
00214       for ( lineIt = lines.constBegin(); lineIt != lines.constEnd(); ++lineIt ) {
00215         if ( !(*lineIt).value().asString().isEmpty() ) {
00216           if ( (*lineIt).hasGroup() )
00217             textLine = (*lineIt).group() + "." + (*lineIt).identifier();
00218           else
00219             textLine = (*lineIt).identifier();
00220 
00221           params = (*lineIt).parameterList();
00222           hasEncoding = false;
00223           if ( params.count() > 0 ) { // we have parameters
00224             for ( paramIt = params.begin(); paramIt != params.end(); ++paramIt ) {
00225               if ( (*paramIt) == "encoding" ) {
00226                 hasEncoding = true;
00227                 encodingType = (*lineIt).parameter( "encoding" ).lower();
00228               }
00229 
00230               values = (*lineIt).parameters( *paramIt );
00231               for ( valueIt = values.constBegin(); valueIt != values.constEnd(); ++valueIt ) {
00232                 textLine.append( ";" + (*paramIt).upper() );
00233                 if ( !(*valueIt).isEmpty() )
00234                   textLine.append( "=" + (*valueIt) );
00235               }
00236             }
00237           }
00238 
00239           if ( hasEncoding ) { // have to encode the data
00240             QByteArray input, output;
00241             if ( encodingType == "b" ) {
00242               input = (*lineIt).value().toByteArray();
00243               KCodecs::base64Encode( input, output );
00244             } else if ( encodingType == "quoted-printable" ) {
00245               input = (*lineIt).value().toString().utf8();
00246               input.resize( input.size() - 1 ); // strip \0
00247               KCodecs::quotedPrintableEncode( input, output, false );
00248             }
00249 
00250             QString value( output );
00251             addEscapes( value );
00252             textLine.append( ":" + value );
00253           } else {
00254             QString value( (*lineIt).value().asString() );
00255             addEscapes( value );
00256             textLine.append( ":" + value );
00257           }
00258 
00259           if ( textLine.length() > FOLD_WIDTH ) { // we have to fold the line
00260             for ( uint i = 0; i <= ( textLine.length() / FOLD_WIDTH ); ++i )
00261               text.append( ( i == 0 ? "" : " " ) + textLine.mid( i * FOLD_WIDTH, FOLD_WIDTH ) + "\r\n" );
00262           } else
00263             text.append( textLine + "\r\n" );
00264         }
00265       }
00266     }
00267 
00268     text.append( "END:VCARD\r\n" );
00269     text.append( "\r\n" );
00270   }
00271 
00272   return text;
00273 }
KDE Home | KDE Accessibility Home | Description of Access Keys