22 #include <tqtextcodec.h>
26 #include "vcardparser.h"
32 static TQString backslash(
"\\\\" );
33 static TQString comma(
"\\," );
34 static TQString newline(
"\\n" );
35 static TQString cr(
"\\r" );
37 static void addEscapes( TQString &str )
39 str.replace(
'\\', backslash );
40 str.replace(
',', comma );
41 str.replace(
'\r', cr );
42 str.replace(
'\n', newline );
45 static void removeEscapes( TQString &str )
47 str.replace( cr,
"\\r" );
48 str.replace( newline,
"\n" );
49 str.replace( comma,
"," );
50 str.replace( backslash,
"\\" );
53 VCardParser::VCardParser()
57 VCardParser::~VCardParser()
61 VCard::List VCardParser::parseVCards(
const TQString& text )
63 static TQRegExp sep(
"[\x0d\x0a]" );
66 VCard::List vCardList;
69 const TQStringList lines = TQStringList::split( sep, text );
70 TQStringList::ConstIterator it;
73 TQStringList::ConstIterator linesEnd( lines.end() );
74 for ( it = lines.begin(); it != linesEnd; ++it ) {
76 if ( (*it).isEmpty() )
79 if ( (*it)[ 0 ] ==
' ' || (*it)[ 0 ] ==
'\t' ) {
80 currentLine += TQString( *it ).remove( 0, 1 );
83 if ( inVCard && !currentLine.isEmpty() ) {
84 int colon = currentLine.find(
':' );
91 const TQString
key = currentLine.left( colon ).stripWhiteSpace();
92 TQString value = currentLine.mid( colon + 1 );
94 TQStringList params = TQStringList::split(
';', key );
97 if ( params[0].
find(
'.' ) != -1 ) {
98 const TQStringList groupList = TQStringList::split(
'.', params[0] );
99 vCardLine.setGroup( groupList[0] );
100 vCardLine.setIdentifier( groupList[1] );
102 vCardLine.setIdentifier( params[0] );
104 if ( params.count() > 1 ) {
105 TQStringList::ConstIterator paramIt = params.begin();
106 for ( ++paramIt; paramIt != params.end(); ++paramIt ) {
107 TQStringList pair = TQStringList::split(
'=', *paramIt );
108 if ( pair.size() == 1 ) {
110 if ( pair[0].lower() ==
"quoted-printable" ) {
111 pair[0] =
"encoding";
112 pair[1] =
"quoted-printable";
113 }
else if ( pair[0].lower() ==
"base64" ) {
114 pair[0] =
"encoding";
117 pair.prepend(
"type" );
121 if ( pair[1].
find(
',' ) != -1 ) {
122 const TQStringList args = TQStringList::split(
',', pair[ 1 ] );
123 TQStringList::ConstIterator argIt;
124 for ( argIt = args.begin(); argIt != args.end(); ++argIt )
125 vCardLine.addParameter( pair[0].lower(), *argIt );
127 vCardLine.addParameter( pair[0].lower(), pair[1] );
131 removeEscapes( value );
134 bool wasBase64Encoded =
false;
136 params = vCardLine.parameterList();
137 if ( params.findIndex(
"encoding" ) != -1 ) {
139 input = TQCString(value.latin1());
140 if ( vCardLine.parameter(
"encoding" ).lower() ==
"b" ||
141 vCardLine.parameter(
"encoding" ).lower() ==
"base64" ) {
143 wasBase64Encoded =
true;
145 else if ( vCardLine.parameter(
"encoding" ).lower() ==
"quoted-printable" ) {
147 while ( value.at( value.length() - 1 ) ==
'=' && it != linesEnd ) {
148 value = value.remove( value.length() - 1, 1 ) + (*it);
151 input = TQCString(value.latin1());
155 output = TQCString(value.utf8());
158 if ( params.findIndex(
"charset" ) != -1 ) {
160 TQTextCodec::codecForName( vCardLine.parameter(
"charset" ).latin1() );
162 vCardLine.setValue( codec->toUnicode( output ) );
164 vCardLine.setValue( TQString(TQString::fromUtf8( output )) );
166 }
else if ( wasBase64Encoded ) {
167 vCardLine.setValue( output );
169 vCardLine.setValue( TQString(TQString::fromUtf8( output )) );
172 currentVCard.addLine( vCardLine );
176 if ( (*it).lower().startsWith(
"begin:vcard" ) ) {
178 currentLine.setLength( 0 );
179 currentVCard.clear();
183 if ( (*it).lower().startsWith(
"end:vcard" ) ) {
185 vCardList.append( currentVCard );
186 currentLine.setLength( 0 );
187 currentVCard.clear();
198 TQString VCardParser::createVCards(
const VCard::List& list )
202 TQString encodingType;
206 TQStringList::ConstIterator identIt;
207 TQStringList::Iterator paramIt;
208 TQStringList::ConstIterator valueIt;
210 VCardLine::List lines;
211 VCardLine::List::ConstIterator lineIt;
212 VCard::List::ConstIterator cardIt;
216 text.reserve( list.size() * 300 );
219 VCard::List::ConstIterator listEnd( list.end() );
220 for ( cardIt = list.begin(); cardIt != listEnd; ++cardIt ) {
221 text.append(
"BEGIN:VCARD\r\n" );
223 idents = (*cardIt).identifiers();
224 for ( identIt = idents.constBegin(); identIt != idents.constEnd(); ++identIt ) {
225 lines = (*cardIt).lines( (*identIt) );
228 for ( lineIt = lines.constBegin(); lineIt != lines.constEnd(); ++lineIt ) {
229 if ( !(*lineIt).value().asString().isEmpty() ) {
230 if ((*lineIt).identifier() != TQString(
"URI")) {
231 if ( (*lineIt).hasGroup() )
232 textLine = (*lineIt).group() +
"." + (*lineIt).identifier();
234 textLine = (*lineIt).identifier();
236 params = (*lineIt).parameterList();
238 if ( params.count() > 0 ) {
239 for ( paramIt = params.begin(); paramIt != params.end(); ++paramIt ) {
240 if ( (*paramIt) ==
"encoding" ) {
242 encodingType = (*lineIt).parameter(
"encoding" ).lower();
245 values = (*lineIt).parameters( *paramIt );
246 for ( valueIt = values.constBegin(); valueIt != values.constEnd(); ++valueIt ) {
247 textLine.append(
";" + (*paramIt).upper() );
248 if ( !(*valueIt).isEmpty() )
249 textLine.append(
"=" + (*valueIt) );
255 TQByteArray input, output;
256 if ( encodingType ==
"b" ) {
257 input = (*lineIt).value().toByteArray();
259 }
else if ( encodingType ==
"quoted-printable" ) {
260 input = (*lineIt).value().toString().utf8();
261 input.resize( input.size() - 1 );
265 TQString value( output );
267 textLine.append(
":" + value );
269 TQString value( (*lineIt).value().asString() );
271 textLine.append(
":" + value );
274 if ( textLine.length() > FOLD_WIDTH ) {
275 for ( uint i = 0; i <= ( textLine.length() / FOLD_WIDTH ); ++i )
276 text.append( ( i == 0 ?
"" :
" " ) + textLine.mid( i * FOLD_WIDTH, FOLD_WIDTH ) +
"\r\n" );
278 text.append( textLine +
"\r\n" );
282 textLine = (*lineIt).identifier();
283 TQString value( (*lineIt).value().asString() );
285 textLine.append(
":" + value );
286 text.append( textLine +
"\r\n" );
292 text.append(
"END:VCARD\r\n" );
293 text.append(
"\r\n" );
static TQCString quotedPrintableDecode(const TQByteArray &in)
static TQCString base64Decode(const TQByteArray &in)
static TQCString base64Encode(const TQByteArray &in, bool insertLFs=false)
static TQCString quotedPrintableEncode(const TQByteArray &in, bool useCRLF=true)
static data, shared by ALL addressee objects
const TDEShortcut & find()