summaryrefslogtreecommitdiffabout
path: root/kabc/vcardparser/vcardparser.cpp
blob: 9ea084d5cdea92c5bf671eecae8ffebac25968f3 (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
/*
    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., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.
*/

#include <qregexp.h>

#include <kmdcodec.h>

#include "vcardparser.h"

#define FOLD_WIDTH 75

using namespace KABC;

VCardParser::VCardParser()
{
}

VCardParser::~VCardParser()
{
}

VCard::List VCardParser::parseVCards( const QString& text )
{
  VCard currentVCard;
  VCard::List vCardList;
  QString currentLine;

  QStringList lines = QStringList::split( QRegExp( "[\x0d\x0a]" ), text );
  QStringList::Iterator it;

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

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

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

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

        QStringList params = QStringList::split( ';', key );
        vCardLine.setIdentifier( params[0] );
        if ( params.count() > 1 ) { // find all parameters
          for ( uint i = 1; i < params.count(); ++i ) {
            QStringList pair = QStringList::split( '=', params[i] );
//US            if ( pair.size() == 1 ) {
            if ( pair.count() == 1 ) {
                pair.prepend( "type" );
            }
            if ( pair[1].contains( ',' ) ) { // parameter in type=x,y,z format
              QStringList args = QStringList::split( ',', pair[ 1 ] );
              for ( uint j = 0; j < args.count(); ++j )
                vCardLine.addParameter( pair[0].lower(), args[j] );
            } else
              vCardLine.addParameter( pair[0].lower(), pair[1] );
          }
        }

        params = vCardLine.parameterList();
        if ( params.contains( "encoding" ) ) { // have to decode the data
#if 0
          QByteArray input, output;
          input = value.local8Bit();
          if ( vCardLine.parameter( "encoding" ).lower() == "b" )
            KCodecs::base64Decode( input, output );
          else if ( vCardLine.parameter( "encoding" ).lower() == "quoted-printable" )
            KCodecs::quotedPrintableDecode( input, output );

          //qDebug("VCardParser::parseVCards has to be verified");
          //US I am not sure if this is correct
          //US          vCardLine.setValue( output );
          QCString cs(output);
          qDebug("len1 %d len2 %d ",input.size(), output.size( ));
#endif
          QCString cs = value.local8Bit();
          qDebug("****************************************** ");
          qDebug("************* WARNING ******************** ");
          qDebug("****************************************** ");
          qDebug("Make sure, the decoding is done after");
          qDebug("QVariant conversion!");
          qDebug("Insert Line DECODING OKAY, where this is implemented");
          // use for decoding the above code!
          vCardLine.setValue( cs );
        } else {
        
            //qDebug("VCardParser::parseVCards has to be verified");
//US          vCardLine.setValue( value.replace( "\\n", "\n" ) );
          vCardLine.setValue( value.replace( QRegExp("\\n"), "\n" ) );
        }

        currentVCard.addLine( vCardLine );
      }
      // we do not save the start and end tag as vcardline
      if ( (*it).lower().startsWith( "begin:vcard" ) ) {
        inVCard = true;
        //qDebug("VCardParser::parseVCards has to be verified");
//US        currentLine.setLength( 0 );
        currentLine = "";
        currentVCard.clear(); // flush vcard
        continue;
      }

      if ( (*it).lower().startsWith( "end:vcard" ) ) {
        inVCard = false;
        vCardList.append( currentVCard );
        //qDebug("VCardParser::parseVCards has to be verified");
//US        currentLine.setLength( 0 );
        currentLine = "";
        currentVCard.clear(); // flush vcard
        continue;
      }

      currentLine = (*it);
    }
  }

  return vCardList;
}

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

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

  bool hasEncoding;

  
  // iterate over the cards
  for ( cardIt = list.begin(); cardIt != list.end(); ++cardIt ) {
    text.append( "BEGIN:VCARD\r\n" );

    idents = (*cardIt).identifiers();
    for ( identIt = idents.begin(); identIt != idents.end(); ++identIt ) {
      VCard card = (*cardIt);
      lines = card.lines( (*identIt) );

      // iterate over the lines
      for ( lineIt = lines.begin(); lineIt != lines.end(); ++lineIt ) {
        if ( !(*lineIt).value().asString().isEmpty() ) {
          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.begin(); valueIt != values.end(); ++valueIt ) {
                textLine.append( ";" + (*paramIt).upper() );
                if ( !(*valueIt).isEmpty() )
                  textLine.append( "=" + (*valueIt) );
              }
            }
          }

          if ( hasEncoding ) { // have to encode the data
            QByteArray input, output;
            
    qDebug("VCardParser::createVCards has to be verified");
//US            input = (*lineIt).value().toByteArray();

//US I am not sure if this is correct    
            QCString cs ((*lineIt).value().toCString());
            input = cs;
            
            if ( encodingType == "b" )
              KCodecs::base64Encode( input, output );
            else if ( encodingType == "quoted-printable" )
              KCodecs::quotedPrintableEncode( input, output );
            textLine.append( ":" + QString( output ) );
          } else {
    qDebug("VCardParser::createVCards has to be verified");
//US            textLine.append( ":" + (*lineIt).value().asString().replace( "\n", "\\n" ) );
            textLine.append( ":" + (*lineIt).value().asString().replace( QRegExp("\n"), "\\n" ) );
          }
          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" );
        }
      }
    }

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

  return text;
}