vcardparser.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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 VCardParser::VCardParser()
00032 {
00033 }
00034
00035 VCardParser::~VCardParser()
00036 {
00037 }
00038
00039 VCard::List VCardParser::parseVCards( const QString& text )
00040 {
00041 VCard currentVCard;
00042 VCard::List vCardList;
00043 QString currentLine;
00044
00045 QStringList lines = QStringList::split( QRegExp( "[\x0d\x0a]" ), text );
00046 QStringList::Iterator it;
00047
00048 bool inVCard = false;
00049 for ( it = lines.begin(); it != lines.end(); ++it ) {
00050
00051 if ( (*it).isEmpty() )
00052 continue;
00053
00054 if ( (*it)[ 0 ] == ' ' || (*it)[ 0 ] == '\t' ) {
00055 currentLine += (*it).remove( 0, 1 );
00056 continue;
00057 } else {
00058 if ( inVCard && !currentLine.isEmpty() ) {
00059 int colon = currentLine.find( ':' );
00060 if ( colon == -1 ) {
00061 currentLine = (*it);
00062 continue;
00063 }
00064
00065 VCardLine vCardLine;
00066 QString key = currentLine.left( colon ).stripWhiteSpace();
00067 QString value = currentLine.mid( colon + 1 );
00068
00069 QStringList params = QStringList::split( ';', key );
00070 vCardLine.setIdentifier( params[0] );
00071 if ( params.count() > 1 ) {
00072 for ( uint i = 1; i < params.count(); ++i ) {
00073 QStringList pair = QStringList::split( '=', params[i] );
00074 if ( pair.size() == 1 ) {
00075
00076 if ( pair[0].lower() == "quoted-printable" ) {
00077 pair[0] = "encoding";
00078 pair[1] = "quoted-printable";
00079 } else {
00080 pair.prepend( "type" );
00081 }
00082 }
00083
00084 if ( pair[1].find( ',' ) != -1 ) {
00085 QStringList args = QStringList::split( ',', pair[ 1 ] );
00086 for ( uint j = 0; j < args.count(); ++j )
00087 vCardLine.addParameter( pair[0].lower(), args[j] );
00088 } else
00089 vCardLine.addParameter( pair[0].lower(), pair[1] );
00090 }
00091 }
00092
00093 params = vCardLine.parameterList();
00094 if ( params.findIndex( "encoding" ) != -1 ) {
00095 QByteArray input, output;
00096 input = value.local8Bit();
00097 if ( vCardLine.parameter( "encoding" ).lower() == "b" )
00098 KCodecs::base64Decode( input, output );
00099 else if ( vCardLine.parameter( "encoding" ).lower() == "quoted-printable" )
00100 KCodecs::quotedPrintableDecode( input, output );
00101
00102 if ( vCardLine.parameter( "charset" ).lower() == "utf-8" ) {
00103 vCardLine.setValue( QString::fromUtf8( output.data(), output.size() ) );
00104 } else {
00105 vCardLine.setValue( output.data() );
00106 }
00107 } else if ( vCardLine.parameter( "charset" ).lower() == "utf-8" ) {
00108 vCardLine.setValue( QString::fromUtf8( value.ascii() ) );
00109 } else
00110 vCardLine.setValue( value.replace( "\\n", "\n" ) );
00111
00112 currentVCard.addLine( vCardLine );
00113 }
00114
00115
00116 if ( (*it).lower().startsWith( "begin:vcard" ) ) {
00117 inVCard = true;
00118 currentLine.setLength( 0 );
00119 currentVCard.clear();
00120 continue;
00121 }
00122
00123 if ( (*it).lower().startsWith( "end:vcard" ) ) {
00124 inVCard = false;
00125 vCardList.append( currentVCard );
00126 currentLine.setLength( 0 );
00127 currentVCard.clear();
00128 continue;
00129 }
00130
00131 currentLine = (*it);
00132 }
00133 }
00134
00135 return vCardList;
00136 }
00137
00138 QString VCardParser::createVCards( const VCard::List& list )
00139 {
00140 QString text;
00141 QString textLine;
00142 QString encodingType;
00143 QStringList idents;
00144 QStringList params;
00145 QStringList values;
00146 QStringList::ConstIterator identIt;
00147 QStringList::Iterator paramIt;
00148 QStringList::Iterator valueIt;
00149
00150 VCardLine::List lines;
00151 VCardLine::List::Iterator lineIt;
00152 VCard::List::ConstIterator cardIt;
00153
00154 bool hasEncoding;
00155
00156
00157
00158 for ( cardIt = list.begin(); cardIt != list.end(); ++cardIt ) {
00159 text.append( "BEGIN:VCARD\r\n" );
00160
00161 idents = (*cardIt).identifiers();
00162 for ( identIt = idents.begin(); identIt != idents.end(); ++identIt ) {
00163 VCard card = (*cardIt);
00164 lines = card.lines( (*identIt) );
00165
00166
00167 for ( lineIt = lines.begin(); lineIt != lines.end(); ++lineIt ) {
00168 if ( !(*lineIt).value().asString().isEmpty() ) {
00169 textLine = (*lineIt).identifier();
00170
00171 params = (*lineIt).parameterList();
00172 hasEncoding = false;
00173 if ( params.count() > 0 ) {
00174 for ( paramIt = params.begin(); paramIt != params.end(); ++paramIt ) {
00175 if ( (*paramIt) == "encoding" ) {
00176 hasEncoding = true;
00177 encodingType = (*lineIt).parameter( "encoding" ).lower();
00178 }
00179
00180 values = (*lineIt).parameters( *paramIt );
00181 for ( valueIt = values.begin(); valueIt != values.end(); ++valueIt ) {
00182 textLine.append( ";" + (*paramIt).upper() );
00183 if ( !(*valueIt).isEmpty() )
00184 textLine.append( "=" + (*valueIt) );
00185 }
00186 }
00187 }
00188
00189 if ( hasEncoding ) {
00190 QByteArray input, output;
00191 input = (*lineIt).value().toByteArray();
00192 if ( encodingType == "b" )
00193 KCodecs::base64Encode( input, output );
00194 else if ( encodingType == "quoted-printable" )
00195 KCodecs::quotedPrintableEncode( input, output );
00196 textLine.append( ":" + QString( output ) );
00197 } else
00198 textLine.append( ":" + (*lineIt).value().asString().replace( "\n", "\\n" ) );
00199
00200 if ( textLine.length() > FOLD_WIDTH ) {
00201 for ( uint i = 0; i <= ( textLine.length() / FOLD_WIDTH ); ++i )
00202 text.append( ( i == 0 ? "" : " " ) + textLine.mid( i * FOLD_WIDTH, FOLD_WIDTH ) + "\r\n" );
00203 } else
00204 text.append( textLine + "\r\n" );
00205 }
00206 }
00207 }
00208
00209 text.append( "END:VCARD\r\n" );
00210 text.append( "\r\n" );
00211 }
00212
00213 return text;
00214 }
This file is part of the documentation for kabc Library Version 3.2.0.