summaryrefslogtreecommitdiffstats
path: root/kabc/vcardparser/vcardparser.cpp
blob: a0e2061a4b80239e6015bef2055ce830e3425c85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
    This file is part of libkabc.
    Copyright (c) 2003 Tobias Koenig <tokoe@kde.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include <tqregexp.h>
#include <tqtextcodec.h>

#include <kmdcodec.h>

#include "vcardparser.h"

#define FOLD_WIDTH 75

using namespace KABC;

static TQString backslash( "\\\\" );
static TQString comma( "\\," );
static TQString newline( "\\n" );
static TQString cr( "\\r" );

static void addEscapes( TQString &str )
{
  str.tqreplace( '\\', backslash );
  str.tqreplace( ',', comma );
  str.tqreplace( '\r', cr );
  str.tqreplace( '\n', newline );
}

static void removeEscapes( TQString &str )
{
  str.tqreplace( cr, "\\r" );
  str.tqreplace( newline, "\n" );
  str.tqreplace( comma, "," );
  str.tqreplace( backslash, "\\" );
}

VCardParser::VCardParser()
{
}

VCardParser::~VCardParser()
{
}

VCard::List VCardParser::parseVCards( const TQString& text )
{
  static TQRegExp sep( "[\x0d\x0a]" );

  VCard currentVCard;
  VCard::List vCardList;
  TQString currentLine;

  const TQStringList lines = TQStringList::split( sep, text );
  TQStringList::ConstIterator it;

  bool inVCard = false;
  TQStringList::ConstIterator linesEnd( lines.end() );
  for ( it = lines.begin(); it != linesEnd; ++it ) {

    if ( (*it).isEmpty() ) // empty line
      continue;

    if ( (*it)[ 0 ] == ' ' || (*it)[ 0 ] == '\t' ) { // folded line => append to previous
      currentLine += TQString( *it ).remove( 0, 1 );
      continue;
    } else {
      if ( inVCard && !currentLine.isEmpty() ) { // now parse the line
        int colon = currentLine.tqfind( ':' );
        if ( colon == -1 ) { // invalid line
          currentLine = (*it);
          continue;
        }

        VCardLine vCardLine;
        const TQString key = currentLine.left( colon ).stripWhiteSpace();
        TQString value = currentLine.mid( colon + 1 );

        TQStringList params = TQStringList::split( ';', key );

        // check for group
        if ( params[0].tqfind( '.' ) != -1 ) {
          const TQStringList groupList = TQStringList::split( '.', params[0] );
          vCardLine.setGroup( groupList[0] );
          vCardLine.setIdentifier( groupList[1] );
        } else
          vCardLine.setIdentifier( params[0] );

        if ( params.count() > 1 ) { // tqfind all parameters
          TQStringList::ConstIterator paramIt = params.begin();
          for ( ++paramIt; paramIt != params.end(); ++paramIt ) {
            TQStringList pair = TQStringList::split( '=', *paramIt );
            if ( pair.size() == 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" );
              }
            }
            // This is pretty much a faster pair[1].tqcontains( ',' )...
            if ( pair[1].tqfind( ',' ) != -1 ) { // parameter in type=x,y,z format
              const TQStringList args = TQStringList::split( ',', pair[ 1 ] );
              TQStringList::ConstIterator argIt;
              for ( argIt = args.begin(); argIt != args.end(); ++argIt )
                vCardLine.addParameter( pair[0].lower(), *argIt );
            } else
              vCardLine.addParameter( pair[0].lower(), pair[1] );
          }
        }

        removeEscapes( value );

        TQByteArray output;
        bool wasBase64Encoded = false;

        params = vCardLine.parameterList();
        if ( params.tqfindIndex( "encoding" ) != -1 ) { // have to decode the data
          TQByteArray input;
          input = TQCString(value.latin1());
          if ( vCardLine.parameter( "encoding" ).lower() == "b" ||
               vCardLine.parameter( "encoding" ).lower() == "base64" ) {
            KCodecs::base64Decode( input, output );
            wasBase64Encoded = true;
          }
          else if ( vCardLine.parameter( "encoding" ).lower() == "quoted-printable" ) {
            // join any qp-folded lines
            while ( value.at( value.length() - 1 ) == '=' && it != linesEnd ) {
              value = value.remove( value.length() - 1, 1 ) + (*it);
              ++it;
            }
            input = TQCString(value.latin1());
            KCodecs::quotedPrintableDecode( input, output );
          }
        } else {
          output = TQCString(value.latin1());
        }

        if ( params.tqfindIndex( "charset" ) != -1 ) { // have to convert the data
          TQTextCodec *codec =
            TQTextCodec::codecForName( vCardLine.parameter( "charset" ).latin1() );
          if ( codec ) {
            vCardLine.setValue( codec->toUnicode( output ) );
          } else {
            vCardLine.setValue( TQString::fromUtf8( output ) );
          }
        } else if ( wasBase64Encoded ) {
            vCardLine.setValue( output );
        } else {  // if charset not given, assume it's in UTF-8 (as used in previous KDE versions)
            vCardLine.setValue( TQString::fromUtf8( output ) );
        }

        currentVCard.addLine( vCardLine );
      }

      // we do not save the start and end tag as vcardline
      if ( (*it).lower().startsWith( "begin:vcard" ) ) {
        inVCard = true;
        currentLine.setLength( 0 );
        currentVCard.clear(); // flush vcard
        continue;
      }

      if ( (*it).lower().startsWith( "end:vcard" ) ) {
        inVCard = false;
        vCardList.append( currentVCard );
        currentLine.setLength( 0 );
        currentVCard.clear(); // flush vcard
        continue;
      }

      currentLine = (*it);
    }
  }

  return vCardList;
}

TQString VCardParser::createVCards( const VCard::List& list )
{
  TQString text;
  TQString textLine;
  TQString encodingType;
  TQStringList idents;
  TQStringList params;
  TQStringList values;
  TQStringList::ConstIterator identIt;
  TQStringList::Iterator paramIt;
  TQStringList::ConstIterator valueIt;

  VCardLine::List lines;
  VCardLine::List::ConstIterator lineIt;
  VCard::List::ConstIterator cardIt;

  bool hasEncoding;

  text.reserve( list.size() * 300 ); // reserve memory to be more efficient

  // iterate over the cards
  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.constBegin(); identIt != idents.constEnd(); ++identIt ) {
      lines = (*cardIt).lines( (*identIt) );

      // iterate over the lines
      for ( lineIt = lines.constBegin(); lineIt != lines.constEnd(); ++lineIt ) {
        if ( !(*lineIt).value().asString().isEmpty() ) {
          if ((*lineIt).identifier() != TQString("URI")) {
            if ( (*lineIt).hasGroup() )
              textLine = (*lineIt).group() + "." + (*lineIt).identifier();
            else
              textLine = (*lineIt).identifier();

            params = (*lineIt).parameterList();
            hasEncoding = false;
            if ( params.count() > 0 ) { // we have parameters
              for ( paramIt = params.begin(); paramIt != params.end(); ++paramIt ) {
                if ( (*paramIt) == "encoding" ) {
                  hasEncoding = true;
                  encodingType = (*lineIt).parameter( "encoding" ).lower();
                }

                values = (*lineIt).parameters( *paramIt );
                for ( valueIt = values.constBegin(); valueIt != values.constEnd(); ++valueIt ) {
                  textLine.append( ";" + (*paramIt).upper() );
                  if ( !(*valueIt).isEmpty() )
                    textLine.append( "=" + (*valueIt) );
                }
              }
            }

            if ( hasEncoding ) { // have to encode the data
              TQByteArray input, output;
              if ( encodingType == "b" ) {
                input = (*lineIt).value().toByteArray();
                KCodecs::base64Encode( input, output );
              } else if ( encodingType == "quoted-printable" ) {
                input = (*lineIt).value().toString().utf8();
                input.resize( input.size() - 1 ); // strip \0
                KCodecs::quotedPrintableEncode( input, output, false );
              }

              TQString value( output );
              addEscapes( value );
              textLine.append( ":" + value );
            } else {
              TQString value( (*lineIt).value().asString() );
              addEscapes( value );
              textLine.append( ":" + value );
            }

            if ( textLine.length() > FOLD_WIDTH ) { // we have to fold the line
              for ( uint i = 0; i <= ( textLine.length() / FOLD_WIDTH ); ++i )
                text.append( ( i == 0 ? "" : " " ) + textLine.mid( i * FOLD_WIDTH, FOLD_WIDTH ) + "\r\n" );
            } else
              text.append( textLine + "\r\n" );
          }
          else {
            // URIs can be full of weird symbols, etc. so bypass all checks
            textLine = (*lineIt).identifier();
            TQString value( (*lineIt).value().asString() );
            addEscapes( value );
            textLine.append( ":" + value );
            text.append( textLine + "\r\n" );
          }
        }
      }
    }

    text.append( "END:VCARD\r\n" );
    text.append( "\r\n" );
  }

  return text;
}