Ejemplo n.º 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;
}
Ejemplo n.º 2
0
Addressee::List VCardTool::parseVCards(const QString &vcard)
{
    static const QChar semicolonSep(';');
    static const QChar commaSep(',');
    QString identifier;

    Addressee::List addrList;
    const VCard::List vCardList = VCardParser::parseVCards(vcard);

    VCard::List::ConstIterator cardIt;
    VCard::List::ConstIterator listEnd(vCardList.end());
    for(cardIt = vCardList.begin(); cardIt != listEnd; ++cardIt)
    {
        Addressee addr;

        const QStringList idents = (*cardIt).identifiers();
        QStringList::ConstIterator identIt;
        QStringList::ConstIterator identEnd(idents.end());
        for(identIt = idents.begin(); identIt != identEnd; ++identIt)
        {
            const VCardLine::List lines = (*cardIt).lines((*identIt));
            VCardLine::List::ConstIterator lineIt;

            // iterate over the lines
            for(lineIt = lines.begin(); lineIt != lines.end(); ++lineIt)
            {
                identifier = (*lineIt).identifier().lower();
                // ADR
                if(identifier == "adr")
                {
                    Address address;
                    const QStringList addrParts = splitString(semicolonSep, (*lineIt).value().asString());
                    if(addrParts.count() > 0)
                        address.setPostOfficeBox(addrParts[0]);
                    if(addrParts.count() > 1)
                        address.setExtended(addrParts[1]);
                    if(addrParts.count() > 2)
                        address.setStreet(addrParts[2]);
                    if(addrParts.count() > 3)
                        address.setLocality(addrParts[3]);
                    if(addrParts.count() > 4)
                        address.setRegion(addrParts[4]);
                    if(addrParts.count() > 5)
                        address.setPostalCode(addrParts[5]);
                    if(addrParts.count() > 6)
                        address.setCountry(addrParts[6]);

                    int type = 0;

                    const QStringList types = (*lineIt).parameters("type");
                    for(QStringList::ConstIterator it = types.begin(); it != types.end(); ++it)
                        type += mAddressTypeMap[(*it).lower()];

                    address.setType(type);
                    addr.insertAddress(address);
                }

                // AGENT
                else if(identifier == "agent")
                    addr.setAgent(parseAgent(*lineIt));

                // BDAY
                else if(identifier == "bday")
                    addr.setBirthday(parseDateTime((*lineIt).value().asString()));

                // CATEGORIES
                else if(identifier == "categories")
                {
                    const QStringList categories = splitString(commaSep, (*lineIt).value().asString());
                    addr.setCategories(categories);
                }

                // CLASS
                else if(identifier == "class")
                    addr.setSecrecy(parseSecrecy(*lineIt));

                // EMAIL
                else if(identifier == "email")
                {
                    const QStringList types = (*lineIt).parameters("type");
                    addr.insertEmail((*lineIt).value().asString(), types.findIndex("PREF") != -1);
                }

                // FN
                else if(identifier == "fn")
                    addr.setFormattedName((*lineIt).value().asString());

                // GEO
                else if(identifier == "geo")
                {
                    Geo geo;

                    const QStringList geoParts = QStringList::split(';', (*lineIt).value().asString(), true);
                    geo.setLatitude(geoParts[0].toFloat());
                    geo.setLongitude(geoParts[1].toFloat());

                    addr.setGeo(geo);
                }

                // KEY
                else if(identifier == "key")
                    addr.insertKey(parseKey(*lineIt));

                // LABEL
                else if(identifier == "label")
                {
                    int type = 0;

                    const QStringList types = (*lineIt).parameters("type");
                    for(QStringList::ConstIterator it = types.begin(); it != types.end(); ++it)
                        type += mAddressTypeMap[(*it).lower()];

                    bool available = false;
                    KABC::Address::List addressList = addr.addresses();
                    KABC::Address::List::Iterator it;
                    for(it = addressList.begin(); it != addressList.end(); ++it)
                    {
                        if((*it).type() == type)
                        {
                            (*it).setLabel((*lineIt).value().asString());
                            addr.insertAddress(*it);
                            available = true;
                            break;
                        }
                    }

                    if(!available)
                    { // a standalone LABEL tag
                        KABC::Address address(type);
                        address.setLabel((*lineIt).value().asString());
                        addr.insertAddress(address);
                    }
                }

                // LOGO
                else if(identifier == "logo")
                    addr.setLogo(parsePicture(*lineIt));

                // MAILER
                else if(identifier == "mailer")
                    addr.setMailer((*lineIt).value().asString());

                // N
                else if(identifier == "n")
                {
                    const QStringList nameParts = splitString(semicolonSep, (*lineIt).value().asString());
                    if(nameParts.count() > 0)
                        addr.setFamilyName(nameParts[0]);
                    if(nameParts.count() > 1)
                        addr.setGivenName(nameParts[1]);
                    if(nameParts.count() > 2)
                        addr.setAdditionalName(nameParts[2]);
                    if(nameParts.count() > 3)
                        addr.setPrefix(nameParts[3]);
                    if(nameParts.count() > 4)
                        addr.setSuffix(nameParts[4]);
                }

                // NAME
                else if(identifier == "name")
                    addr.setName((*lineIt).value().asString());

                // NICKNAME
                else if(identifier == "nickname")
                    addr.setNickName((*lineIt).value().asString());

                // NOTE
                else if(identifier == "note")
                    addr.setNote((*lineIt).value().asString());

                // ORGANIZATION
                else if(identifier == "org")
                {
                    const QStringList orgParts = splitString(semicolonSep, (*lineIt).value().asString());
                    if(orgParts.count() > 0)
                        addr.setOrganization(orgParts[0]);
                    if(orgParts.count() > 1)
                        addr.setDepartment(orgParts[1]);
                }

                // PHOTO
                else if(identifier == "photo")
                    addr.setPhoto(parsePicture(*lineIt));

                // PROID
                else if(identifier == "prodid")
                    addr.setProductId((*lineIt).value().asString());

                // REV
                else if(identifier == "rev")
                    addr.setRevision(parseDateTime((*lineIt).value().asString()));

                // ROLE
                else if(identifier == "role")
                    addr.setRole((*lineIt).value().asString());

                // SORT-STRING
                else if(identifier == "sort-string")
                    addr.setSortString((*lineIt).value().asString());

                // SOUND
                else if(identifier == "sound")
                    addr.setSound(parseSound(*lineIt));

                // TEL
                else if(identifier == "tel")
                {
                    PhoneNumber phone;
                    phone.setNumber((*lineIt).value().asString());

                    int type = 0;

                    const QStringList types = (*lineIt).parameters("type");
                    for(QStringList::ConstIterator it = types.begin(); it != types.end(); ++it)
                        type += mPhoneTypeMap[(*it).upper()];

                    phone.setType(type);

                    addr.insertPhoneNumber(phone);
                }

                // TITLE
                else if(identifier == "title")
                    addr.setTitle((*lineIt).value().asString());

                // TZ
                else if(identifier == "tz")
                {
                    TimeZone tz;
                    const QString date = (*lineIt).value().asString();

                    int hours = date.mid(1, 2).toInt();
                    int minutes = date.mid(4, 2).toInt();
                    int offset = (hours * 60) + minutes;
                    offset = offset * (date[0] == '+' ? 1 : -1);

                    tz.setOffset(offset);
                    addr.setTimeZone(tz);
                }

                // UID
                else if(identifier == "uid")
                    addr.setUid((*lineIt).value().asString());

                // URL
                else if(identifier == "url")
                    addr.setUrl(KURL((*lineIt).value().asString()));

                // X-
                else if(identifier.startsWith("x-"))
                {
                    const QString key = (*lineIt).identifier().mid(2);
                    int dash = key.find("-");
                    addr.insertCustom(key.left(dash), key.mid(dash + 1), (*lineIt).value().asString());
                }
            }
        }

        addrList.append(addr);
    }

    return addrList;
}
Ejemplo n.º 3
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;
}