Beispiel #1
0
QByteArray VCardParser::createVCards( const VCard::List &list )
{
  QByteArray text;
  QByteArray textLine;
  QString encodingType;
  QStringList idents;
  QStringList params;
  QStringList values;
  QStringList::ConstIterator identIt;
  QStringList::Iterator paramIt;
  QStringList::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 ) {
        QVariant val = ( *lineIt ).value();
        if ( val.isValid() ) {
          if ( ( *lineIt ).hasGroup() ) {
            textLine = ( *lineIt ).group().toLatin1() + '.' + ( *lineIt ).identifier().toLatin1();
          } else {
            textLine = ( *lineIt ).identifier().toLatin1();
          }

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

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

          QByteArray input, output;

          // handle charset
          if ( ( *lineIt ).parameterList().contains( QLatin1String( "charset" ) ) ) {
            // have to convert the data
            const QString value = ( *lineIt ).value().toString();
            QTextCodec *codec = QTextCodec::codecForName(
              ( *lineIt ).parameter( QLatin1String( "charset" ) ).toLatin1() );
            if ( codec ) {
              input = codec->fromUnicode( value );
            } else {
              input = value.toUtf8();
            }
          } else if ( ( *lineIt ).value().type() == QVariant::ByteArray ) {
            input = ( *lineIt ).value().toByteArray();
          } else {
            input = ( *lineIt ).value().toString().toUtf8();
          }

          // handle encoding
          if ( hasEncoding ) { // have to encode the data
            if ( encodingType == QLatin1String( "b" ) ) {
              output = input.toBase64();
            } else if ( encodingType == QLatin1String( "quoted-printable" ) ) {
              KCodecs::quotedPrintableEncode( input, output, false );
            }
          } else {
            output = input;
          }
          addEscapes( output, ( *lineIt ).identifier() == QLatin1String( "CATEGORIES" ) );

          if ( !output.isEmpty() ) {
            textLine.append( ':' + output );

            if ( textLine.length() > FOLD_WIDTH ) { // we have to fold the line
              for ( int 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;
}
Beispiel #2
0
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::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).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
            QByteArray 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 );
            }

            QString value( output );
            addEscapes( value );
            textLine.append( ":" + value );
          } else {
            QString 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" );
        }
      }
    }

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

  return text;
}