Esempio n. 1
0
bool ResourceSql::save(Ticket *)
{
    // we have to delete all entries for this user and reinsert them
    QSqlQuery query("select addressId from kaddressbook_main_" + mUser);

    while(query.next())
    {
        QString addrId = query.value(0).toString();
        QSqlQuery q;

        q.exec("DELETE FROM kaddressbook_emails WHERE addressId = '" + addrId + "'");
        q.exec("DELETE FROM kaddressbook_phones WHERE addressId = '" + addrId + "'");
        q.exec("DELETE FROM kaddressbook_addresses WHERE addressId = '" + addrId + "'");
        q.exec("DELETE FROM kaddressbook_categories WHERE addressId = '" + addrId + "'");
        q.exec("DELETE FROM kaddressbook_customs WHERE addressId = '" + addrId + "'");

        q.exec("DELETE FROM kaddressbook_main_" + mUser + " WHERE addressId = '" + addrId + "'");
    }

    // let's start...
    AddressBook::Iterator it;
    for(it = addressBook()->begin(); it != addressBook()->end(); ++it)
    {
        if((*it).resource() != this && (*it).resource() != 0) // save only my and new entries
            continue;

        QString uid = (*it).uid();

        query.exec("INSERT INTO kaddressbook_main_" + mUser + " VALUES ('" + (*it).uid() + "','" + (*it).name() + "','" + (*it).familyName() + "','"
                   + (*it).givenName() + "','" + (*it).additionalName() + "','" + (*it).prefix() + "','" + (*it).suffix() + "','" + (*it).nickName()
                   + "','" + (*it).birthday().toString(Qt::ISODate) + "','" + (*it).mailer() + "','" + QString::number((*it).timeZone().offset())
                   + "','" + QString::number((*it).geo().latitude()) + "','" + QString::number((*it).geo().longitude()) + "','" + (*it).title()
                   + "','" + (*it).role() + "','" + (*it).organization() + "','" + (*it).note() + "','" + (*it).productId() + "','"
                   + (*it).revision().toString(Qt::ISODate) + "','" + (*it).sortString() + "','" + (*it).url().url() + "')");

        // emails
        {
            QStringList emails = (*it).emails();
            QStringList::ConstIterator it;
            bool preferred = true;
            for(it = emails.begin(); it != emails.end(); ++it)
            {
                query.exec("INSERT INTO kaddressbook_emails VALUES ('" + uid + "','" + (*it) + "','" + QString::number(preferred) + "')");
                preferred = false;
            }
        }

        // phonenumbers
        {
            PhoneNumber::List phoneNumberList = (*it).phoneNumbers();
            PhoneNumber::List::ConstIterator it;
            for(it = phoneNumberList.begin(); it != phoneNumberList.end(); ++it)
            {
                query.exec("INSERT INTO kaddressbook_phones VALUES ('" + uid + "','" + (*it).number() + "','" + QString::number((*it).type()) + "')");
            }
        }

        // postal addresses
        {
            Address::List addressList = (*it).addresses();
            Address::List::ConstIterator it;
            for(it = addressList.begin(); it != addressList.end(); ++it)
            {
                query.exec("INSERT INTO kaddressbook_addresses VALUES ('" + uid + "','" + (*it).postOfficeBox() + "','" + (*it).extended() + "','"
                           + (*it).street() + "','" + (*it).locality() + "','" + (*it).region() + "','" + (*it).postalCode() + "','" + (*it).country()
                           + "','" + (*it).label() + "','" + QString::number((*it).type()) + "')");
            }
        }

        // categories
        {
            QStringList categories = (*it).categories();
            QStringList::ConstIterator it;
            for(it = categories.begin(); it != categories.end(); ++it)
                query.exec("INSERT INTO kaddressbook_categories VALUES ('" + uid + "','" + (*it) + "')");
        }

        // customs
        {
            QStringList list = (*it).customs();
            QStringList::ConstIterator it;
            for(it = list.begin(); it != list.end(); ++it)
            {
                int dashPos = (*it).find('-');
                int colonPos = (*it).find(':');
                QString app = (*it).left(dashPos);
                QString name = (*it).mid(dashPos + 1, colonPos - dashPos - 1);
                QString value = (*it).right((*it).length() - colonPos - 1);

                query.exec("INSERT INTO kaddressbook_categories VALUES ('" + uid + "','" + app + "','" + name + "','" + value + "')");
            }
        }
    }

    return true;
}
Esempio n. 2
0
void ResourceSlox::createAddresseeFields( QDomDocument &doc, QDomElement &prop,
                                          const Addressee &a )
{
  // choose addressbook
  WebdavHandler::addSloxElement( this, doc, prop, fieldName( FolderId ), mPrefs->folderId() );

  // person
  WebdavHandler::addSloxElement( this, doc, prop, fieldName( GivenName ), a.givenName() );
  WebdavHandler::addSloxElement( this, doc, prop, fieldName( FamilyName ), a.familyName() );
  WebdavHandler::addSloxElement( this, doc, prop, fieldName( Title ), a.title() );
  if ( !a.birthday().isNull() )
    WebdavHandler::addSloxElement( this, doc, prop, fieldName( Birthday ),
                                   WebdavHandler::qDateTimeToSlox( a.birthday() ) );
  else
    WebdavHandler::addSloxElement( this, doc, prop, fieldName( Birthday ) );
  WebdavHandler::addSloxElement( this, doc, prop, fieldName( Role ), a.role() );
  WebdavHandler::addSloxElement( this, doc, prop, fieldName( Department ),
                                 a.custom( "KADDRESSBOOK", "X-Department" ) );
  if ( type() == "ox" ) { // OX only fields
    WebdavHandler::addSloxElement( this, doc, prop, fieldName( DisplayName ), a.formattedName() );
    WebdavHandler::addSloxElement( this, doc, prop, fieldName( SecondName ), a.additionalName() );
    WebdavHandler::addSloxElement( this, doc, prop, fieldName( Suffix ), a.suffix() );
    WebdavHandler::addSloxElement( this, doc, prop, fieldName( Organization ), a.organization() );
    WebdavHandler::addSloxElement( this, doc, prop, fieldName( NickName ), a.nickName() );
    WebdavHandler::addSloxElement( this, doc, prop, fieldName( InstantMsg ),
                                   a.custom( "KADDRESSBOOK", "X-IMAddress" ) );
    WebdavHandler::addSloxElement( this, doc, prop, fieldName( Office ),
                                   a.custom( "KADDRESSBOOK", "X-Office" ) );
    WebdavHandler::addSloxElement( this, doc, prop, fieldName( Profession ),
                                   a.custom( "KADDRESSBOOK", "X-Profession" ) );
    WebdavHandler::addSloxElement( this, doc, prop, fieldName( ManagersName ),
                                   a.custom( "KADDRESSBOOK", "X-ManagersName" ) );
    WebdavHandler::addSloxElement( this, doc, prop, fieldName( AssistantsName ),
                                   a.custom( "KADDRESSBOOK", "X-AssistantsName" ) );
    WebdavHandler::addSloxElement( this, doc, prop, fieldName( SpousesName ),
                                   a.custom( "KADDRESSBOOK", "X-SpousesName" ) );
    QString anniversary = a.custom( "KADDRESSBOOK", "X-Anniversary" );
    if ( !anniversary.isEmpty() )
      WebdavHandler::addSloxElement( this, doc, prop, fieldName( Anniversary ),
        WebdavHandler::qDateTimeToSlox( QDateTime::fromString( anniversary, Qt::ISODate ).date() ) );
    else
      WebdavHandler::addSloxElement( this, doc, prop, fieldName( Anniversary ) );
  }

  WebdavHandler::addSloxElement( this, doc, prop, fieldName( Url ), a.url().url() );
  WebdavHandler::addSloxElement( this, doc, prop, fieldName( Comment ), a.note() );
  WebdavHandler::addSloxElement( this, doc, prop, fieldName( Categories ), a.categories().join( ", " ) );

  // emails
  QStringList email_list = a.emails();
  QStringList::const_iterator emails_it = email_list.begin();
  if ( emails_it != email_list.end() )
    WebdavHandler::addSloxElement( this, doc, prop, fieldName( PrimaryEmail ), *(emails_it++) );
  if ( emails_it != email_list.end() )
    WebdavHandler::addSloxElement( this, doc, prop, fieldName( SecondaryEmail1 ), *(emails_it++) );
  if ( emails_it != email_list.end() )
    WebdavHandler::addSloxElement( this, doc, prop, fieldName( SecondaryEmail2 ), *(emails_it++) );

  // phone numbers
  PhoneNumber::List pnlist = a.phoneNumbers();
  QMap<int, QStringList> pnSaveMap;
  if ( type() == "ox" )
    pnSaveMap = mPhoneNumberOxMap;
  else
    pnSaveMap = mPhoneNumberSloxMap;
  for ( PhoneNumber::List::ConstIterator it = pnlist.begin() ; it != pnlist.end(); ++it ) {
    if ( pnSaveMap.contains( (*it).type() ) ) {
      QStringList l = pnSaveMap[(*it).type()];
      QString fn = l.first();
      l.remove( l.begin() );
      if ( !l.isEmpty() )
        pnSaveMap[(*it).type()] = l;
      else
        pnSaveMap.remove( (*it).type() );
      WebdavHandler::addSloxElement( this, doc, prop, fn, (*it).number() );
    } else
      kdDebug() << k_funcinfo << "Can't save phone number " << (*it).number() << " of type " << (*it).type() << endl;
  }
  // send empty fields for the remaining ohone number fields
  // it's not possible to delete phone numbers otherwise
  for ( QMap<int, QStringList>::ConstIterator it = pnSaveMap.begin(); it != pnSaveMap.end(); ++it ) {
    QStringList l = it.data();
    for ( QStringList::ConstIterator it2 = l.begin(); it2 != l.end(); ++it2 )
      WebdavHandler::addSloxElement( this, doc, prop, (*it2) );
  }

  // write addresses
  createAddressFields( doc, prop, fieldName( HomePrefix ), a.address( KABC::Address::Home ) );
  if ( type() == "ox" ) {
    createAddressFields( doc, prop, fieldName( BusinessPrefix ), a.address( KABC::Address::Work ) );
    createAddressFields( doc, prop, fieldName( OtherPrefix ), a.address( 0 ) );
  }
}
Esempio n. 3
0
// TODO: make list a const&
QString VCardTool::createVCards(Addressee::List list, VCard::Version version)
{
    VCard::List vCardList;

    Addressee::List::ConstIterator addrIt;
    Addressee::List::ConstIterator listEnd(list.constEnd());
    for(addrIt = list.constBegin(); addrIt != listEnd; ++addrIt)
    {
        VCard card;
        QStringList::ConstIterator strIt;

        // ADR + LABEL
        const Address::List addresses = (*addrIt).addresses();
        for(Address::List::ConstIterator it = addresses.begin(); it != addresses.end(); ++it)
        {
            QStringList address;

            bool isEmpty = ((*it).postOfficeBox().isEmpty() && (*it).extended().isEmpty() && (*it).street().isEmpty() && (*it).locality().isEmpty()
                            && (*it).region().isEmpty() && (*it).postalCode().isEmpty() && (*it).country().isEmpty());

            address.append((*it).postOfficeBox().replace(';', "\\;"));
            address.append((*it).extended().replace(';', "\\;"));
            address.append((*it).street().replace(';', "\\;"));
            address.append((*it).locality().replace(';', "\\;"));
            address.append((*it).region().replace(';', "\\;"));
            address.append((*it).postalCode().replace(';', "\\;"));
            address.append((*it).country().replace(';', "\\;"));

            VCardLine adrLine("ADR", address.join(";"));
            if(version == VCard::v2_1 && needsEncoding(address.join(";")))
            {
                adrLine.addParameter("charset", "UTF-8");
                adrLine.addParameter("encoding", "QUOTED-PRINTABLE");
            }

            VCardLine labelLine("LABEL", (*it).label());
            if(version == VCard::v2_1 && needsEncoding((*it).label()))
            {
                labelLine.addParameter("charset", "UTF-8");
                labelLine.addParameter("encoding", "QUOTED-PRINTABLE");
            }

            bool hasLabel = !(*it).label().isEmpty();
            QMap< QString, int >::ConstIterator typeIt;
            for(typeIt = mAddressTypeMap.constBegin(); typeIt != mAddressTypeMap.constEnd(); ++typeIt)
            {
                if(typeIt.data() & (*it).type())
                {
                    adrLine.addParameter("TYPE", typeIt.key());
                    if(hasLabel)
                        labelLine.addParameter("TYPE", typeIt.key());
                }
            }

            if(!isEmpty)
                card.addLine(adrLine);
            if(hasLabel)
                card.addLine(labelLine);
        }

        // AGENT
        card.addLine(createAgent(version, (*addrIt).agent()));

        // BDAY
        card.addLine(VCardLine("BDAY", createDateTime((*addrIt).birthday())));

        // CATEGORIES
        if(version == VCard::v3_0)
        {
            QStringList categories = (*addrIt).categories();
            QStringList::Iterator catIt;
            for(catIt = categories.begin(); catIt != categories.end(); ++catIt)
                (*catIt).replace(',', "\\,");

            VCardLine catLine("CATEGORIES", categories.join(","));
            if(version == VCard::v2_1 && needsEncoding(categories.join(",")))
            {
                catLine.addParameter("charset", "UTF-8");
                catLine.addParameter("encoding", "QUOTED-PRINTABLE");
            }

            card.addLine(catLine);
        }

        // CLASS
        if(version == VCard::v3_0)
        {
            card.addLine(createSecrecy((*addrIt).secrecy()));
        }

        // EMAIL
        const QStringList emails = (*addrIt).emails();
        bool pref = true;
        for(strIt = emails.begin(); strIt != emails.end(); ++strIt)
        {
            VCardLine line("EMAIL", *strIt);
            if(pref == true && emails.count() > 1)
            {
                line.addParameter("TYPE", "PREF");
                pref = false;
            }
            card.addLine(line);
        }

        // FN
        VCardLine fnLine("FN", (*addrIt).formattedName());
        if(version == VCard::v2_1 && needsEncoding((*addrIt).formattedName()))
        {
            fnLine.addParameter("charset", "UTF-8");
            fnLine.addParameter("encoding", "QUOTED-PRINTABLE");
        }
        card.addLine(fnLine);

        // GEO
        Geo geo = (*addrIt).geo();
        if(geo.isValid())
        {
            QString str;
            str.sprintf("%.6f;%.6f", geo.latitude(), geo.longitude());
            card.addLine(VCardLine("GEO", str));
        }

        // KEY
        const Key::List keys = (*addrIt).keys();
        Key::List::ConstIterator keyIt;
        for(keyIt = keys.begin(); keyIt != keys.end(); ++keyIt)
            card.addLine(createKey(*keyIt));

        // LOGO
        card.addLine(createPicture("LOGO", (*addrIt).logo()));

        // MAILER
        VCardLine mailerLine("MAILER", (*addrIt).mailer());
        if(version == VCard::v2_1 && needsEncoding((*addrIt).mailer()))
        {
            mailerLine.addParameter("charset", "UTF-8");
            mailerLine.addParameter("encoding", "QUOTED-PRINTABLE");
        }
        card.addLine(mailerLine);

        // N
        QStringList name;
        name.append((*addrIt).familyName().replace(';', "\\;"));
        name.append((*addrIt).givenName().replace(';', "\\;"));
        name.append((*addrIt).additionalName().replace(';', "\\;"));
        name.append((*addrIt).prefix().replace(';', "\\;"));
        name.append((*addrIt).suffix().replace(';', "\\;"));

        VCardLine nLine("N", name.join(";"));
        if(version == VCard::v2_1 && needsEncoding(name.join(";")))
        {
            nLine.addParameter("charset", "UTF-8");
            nLine.addParameter("encoding", "QUOTED-PRINTABLE");
        }
        card.addLine(nLine);

        // NAME
        VCardLine nameLine("NAME", (*addrIt).name());
        if(version == VCard::v2_1 && needsEncoding((*addrIt).name()))
        {
            nameLine.addParameter("charset", "UTF-8");
            nameLine.addParameter("encoding", "QUOTED-PRINTABLE");
        }
        card.addLine(nameLine);

        // NICKNAME
        if(version == VCard::v3_0)
            card.addLine(VCardLine("NICKNAME", (*addrIt).nickName()));

        // NOTE
        VCardLine noteLine("NOTE", (*addrIt).note());
        if(version == VCard::v2_1 && needsEncoding((*addrIt).note()))
        {
            noteLine.addParameter("charset", "UTF-8");
            noteLine.addParameter("encoding", "QUOTED-PRINTABLE");
        }
        card.addLine(noteLine);

        // ORG
        QStringList organization;
        organization.append((*addrIt).organization().replace(';', "\\;"));
        if(!(*addrIt).department().isEmpty())
            organization.append((*addrIt).department().replace(';', "\\;"));
        VCardLine orgLine("ORG", organization.join(";"));
        if(version == VCard::v2_1 && needsEncoding(organization.join(";")))
        {
            orgLine.addParameter("charset", "UTF-8");
            orgLine.addParameter("encoding", "QUOTED-PRINTABLE");
        }
        card.addLine(orgLine);

        // PHOTO
        card.addLine(createPicture("PHOTO", (*addrIt).photo()));

        // PROID
        if(version == VCard::v3_0)
            card.addLine(VCardLine("PRODID", (*addrIt).productId()));

        // REV
        card.addLine(VCardLine("REV", createDateTime((*addrIt).revision())));

        // ROLE
        VCardLine roleLine("ROLE", (*addrIt).role());
        if(version == VCard::v2_1 && needsEncoding((*addrIt).role()))
        {
            roleLine.addParameter("charset", "UTF-8");
            roleLine.addParameter("encoding", "QUOTED-PRINTABLE");
        }
        card.addLine(roleLine);

        // SORT-STRING
        if(version == VCard::v3_0)
            card.addLine(VCardLine("SORT-STRING", (*addrIt).sortString()));

        // SOUND
        card.addLine(createSound((*addrIt).sound()));

        // TEL
        const PhoneNumber::List phoneNumbers = (*addrIt).phoneNumbers();
        PhoneNumber::List::ConstIterator phoneIt;
        for(phoneIt = phoneNumbers.begin(); phoneIt != phoneNumbers.end(); ++phoneIt)
        {
            VCardLine line("TEL", (*phoneIt).number());

            QMap< QString, int >::ConstIterator typeIt;
            for(typeIt = mPhoneTypeMap.constBegin(); typeIt != mPhoneTypeMap.constEnd(); ++typeIt)
            {
                if(typeIt.data() & (*phoneIt).type())
                    line.addParameter("TYPE", typeIt.key());
            }

            card.addLine(line);
        }

        // TITLE
        VCardLine titleLine("TITLE", (*addrIt).title());
        if(version == VCard::v2_1 && needsEncoding((*addrIt).title()))
        {
            titleLine.addParameter("charset", "UTF-8");
            titleLine.addParameter("encoding", "QUOTED-PRINTABLE");
        }
        card.addLine(titleLine);

        // TZ
        TimeZone timeZone = (*addrIt).timeZone();
        if(timeZone.isValid())
        {
            QString str;

            int neg = 1;
            if(timeZone.offset() < 0)
                neg = -1;

            str.sprintf("%c%02d:%02d", (timeZone.offset() >= 0 ? '+' : '-'), (timeZone.offset() / 60) * neg, (timeZone.offset() % 60) * neg);

            card.addLine(VCardLine("TZ", str));
        }

        // UID
        card.addLine(VCardLine("UID", (*addrIt).uid()));

        // URL
        card.addLine(VCardLine("URL", (*addrIt).url().url()));

        // VERSION
        if(version == VCard::v2_1)
            card.addLine(VCardLine("VERSION", "2.1"));
        if(version == VCard::v3_0)
            card.addLine(VCardLine("VERSION", "3.0"));

        // X-
        const QStringList customs = (*addrIt).customs();
        for(strIt = customs.begin(); strIt != customs.end(); ++strIt)
        {
            QString identifier = "X-" + (*strIt).left((*strIt).find(":"));
            QString value = (*strIt).mid((*strIt).find(":") + 1);
            if(value.isEmpty())
                continue;

            VCardLine line(identifier, value);
            if(version == VCard::v2_1 && needsEncoding(value))
            {
                line.addParameter("charset", "UTF-8");
                line.addParameter("encoding", "QUOTED-PRINTABLE");
            }
            card.addLine(line);
        }

        vCardList.append(card);
    }

    return VCardParser::createVCards(vCardList);
}
Esempio n. 4
0
void VCardFormatImpl::saveAddressee( const Addressee &addressee, VCARD::VCard *v, bool intern )
{
  ContentLine cl;
  QString value;

  addTextValue( v, EntityName, addressee.name() );
  addTextValue( v, EntityUID, addressee.uid() );
  addTextValue( v, EntityFullName, addressee.formattedName() );

  QStringList emails = addressee.emails();
  QStringList::ConstIterator it4;
  for( it4 = emails.begin(); it4 != emails.end(); ++it4 ) {
    addTextValue( v, EntityEmail, *it4 );
  }

  QStringList customs = addressee.customs();
  QStringList::ConstIterator it5;
  for( it5 = customs.begin(); it5 != customs.end(); ++it5 ) {
    addCustomValue( v, *it5 );
  }

  addTextValue( v, EntityURL, addressee.url().url() );

  addNValue( v, addressee );

  addTextValue( v, EntityNickname, addressee.nickName() );
  addTextValue( v, EntityMailer, addressee.mailer() );
  addTextValue( v, EntityTitle, addressee.title() );
  addTextValue( v, EntityRole, addressee.role() );
  addTextValue( v, EntityOrganisation, addressee.organization() );
  addTextValue( v, EntityNote, addressee.note() );
  addTextValue( v, EntityProductID, addressee.productId() );
  addTextValue( v, EntitySortString, addressee.sortString() );

  Address::List addresses = addressee.addresses();
  Address::List::ConstIterator it3;
  for( it3 = addresses.begin(); it3 != addresses.end(); ++it3 ) {
    addAddressValue( v, *it3 );
    addLabelValue( v, *it3 );
  }

  PhoneNumber::List phoneNumbers = addressee.phoneNumbers();
  PhoneNumber::List::ConstIterator it2;
  for( it2 = phoneNumbers.begin(); it2 != phoneNumbers.end(); ++it2 ) {
    addTelephoneValue( v, *it2 );
  }

  Key::List keys = addressee.keys();
  Key::List::ConstIterator it6;
  for( it6 = keys.begin(); it6 != keys.end(); ++it6 ) {
    addKeyValue( v, *it6 );
  }

  addTextValue( v, EntityCategories, addressee.categories().join(",") );

  addDateValue( v, EntityBirthday, addressee.birthday().date() );
  addDateTimeValue( v, EntityRevision, addressee.revision() );
  addGeoValue( v, addressee.geo() );
  addUTCValue( v, addressee.timeZone() );

  addClassValue( v, addressee.secrecy() );

  addPictureValue( v, EntityPhoto, addressee.photo(), addressee, intern );
  addPictureValue( v, EntityLogo, addressee.logo(), addressee, intern );

  addAgentValue( v, addressee.agent() );

  addSoundValue( v, addressee.sound(), addressee, intern );
}