summaryrefslogtreecommitdiffabout
Side-by-side diff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--kabc/converter/opie/opieconverterE.pro2
-rw-r--r--kabc/formats/vcardformatplugin2.cpp4
-rw-r--r--kabc/vcardformatimpl.cpp48
-rw-r--r--kabc/vcardformatplugin.cpp4
-rw-r--r--kabc/vcardparser/vcardtool.cpp22
5 files changed, 53 insertions, 27 deletions
diff --git a/kabc/converter/opie/opieconverterE.pro b/kabc/converter/opie/opieconverterE.pro
index 85ba28f..e026f2a 100644
--- a/kabc/converter/opie/opieconverterE.pro
+++ b/kabc/converter/opie/opieconverterE.pro
@@ -1,32 +1,32 @@
TEMPLATE = lib
-INCLUDEPATH += . ../.. ../../../microkde ../../../microkde/kdecore $(QPEDIR)/include $(OPIEDIR)/include
+INCLUDEPATH += . ../.. ../../../microkde ../../../microkde/kdecore $(OPIEDIR)/include $(OPEDIR)/include
#CONFIG += staticlib
OBJECTS_DIR = obj/$(PLATFORM)
MOC_DIR = moc/$(PLATFORM)
#for static linkage, put it here
#DESTDIR=../../lib/$(PLATFORM)
#for dynamic linkage, put it here
DESTDIR=$(QPEDIR)/lib
LIBS += -lmicrokde
LIBS += -lmicrokabc
LIBS += -L$(QPEDIR)/lib
LIBS += -L$(OPIEDIR)/lib
LIBS += -lopie
LIBS += -lqpe
LIBS += -lqte
TARGET = microkabc_opieconverter
# Input
HEADERS += \
opieconverter.h \
SOURCES += \
opieconverter.cpp \
diff --git a/kabc/formats/vcardformatplugin2.cpp b/kabc/formats/vcardformatplugin2.cpp
index f19e218..41b0c9a 100644
--- a/kabc/formats/vcardformatplugin2.cpp
+++ b/kabc/formats/vcardformatplugin2.cpp
@@ -1,120 +1,124 @@
/*
Enhanced Version of the file for platform independent KDE tools.
Copyright (c) 2004 Ulf Schenk
$Id$
*/
#include "vcardformatplugin2.h"
#include "address.h"
#include "addressee.h"
#include "vcardparser/vcardtool.h"
#include <qtextstream.h>
#include <qfile.h>
using namespace KABC;
extern "C"
{
FormatPlugin *format()
{
return new VCardFormatPlugin2();
}
}
VCardFormatPlugin2::VCardFormatPlugin2()
{
}
VCardFormatPlugin2::~VCardFormatPlugin2()
{
}
bool VCardFormatPlugin2::load( Addressee &addressee, QFile *file )
{
+ qDebug("VCardFormatPlugin2::load");
QString data;
QTextStream t( file );
t.setEncoding( QTextStream::UnicodeUTF8 );
data = t.read();
VCardTool tool;
Addressee::List l = tool.parseVCards( data );
if ( ! l.first().isEmpty() ) {
addressee = l.first();
return true;
}
return false;
}
bool VCardFormatPlugin2::loadAll( AddressBook *addressBook, Resource *resource, QFile *file )
{
+ qDebug("VCardFormatPlugin2::loadAll");
QString data;
QTextStream t( file );
t.setEncoding( QTextStream::UnicodeUTF8 );
data = t.read();
VCardTool tool;
Addressee::List l = tool.parseVCards( data );
Addressee::List::Iterator itr;
for ( itr = l.begin(); itr != l.end(); ++itr) {
Addressee addressee = *itr;
addressee.setResource( resource );
addressBook->insertAddressee( addressee );
}
return true;
}
void VCardFormatPlugin2::save( const Addressee &addressee, QFile *file )
{
+ qDebug("VCardFormatPlugin2::save");
VCardTool tool;
Addressee::List vcardlist;
vcardlist.append( addressee );
QTextStream t( file );
t.setEncoding( QTextStream::UnicodeUTF8 );
t << tool.createVCards( vcardlist );
}
void VCardFormatPlugin2::saveAll( AddressBook *ab, Resource *resource, QFile *file )
{
+ qDebug("VCardFormatPlugin2::saveAll");
VCardTool tool;
Addressee::List vcardlist;
AddressBook::Iterator it;
for ( it = ab->begin(); it != ab->end(); ++it ) {
if ( (*it).resource() == resource ) {
(*it).setChanged( false );
vcardlist.append( *it );
}
}
QTextStream t( file );
t.setEncoding( QTextStream::UnicodeUTF8 );
t << tool.createVCards( vcardlist );
}
bool VCardFormatPlugin2::checkFormat( QFile *file ) const
{
QString line;
file->readLine( line, 1024 );
line = line.stripWhiteSpace();
if ( line == "BEGIN:VCARD" )
return true;
else
return false;
}
diff --git a/kabc/vcardformatimpl.cpp b/kabc/vcardformatimpl.cpp
index f90f813..3fcaf94 100644
--- a/kabc/vcardformatimpl.cpp
+++ b/kabc/vcardformatimpl.cpp
@@ -1,1023 +1,1027 @@
/*
This file is part of libkabc.
Copyright (c) 2001 Cornelius Schumacher <schumacher@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.
*/
/*
Enhanced Version of the file for platform independent KDE tools.
Copyright (c) 2004 Ulf Schenk
$Id$
*/
#include <qfile.h>
#include <qregexp.h>
#include <kdebug.h>
#include <kmdcodec.h>
#include <kstandarddirs.h>
#include <ktempfile.h>
#include <VCard.h>
#include "addressbook.h"
#include "vcardformatimpl.h"
using namespace KABC;
using namespace VCARD;
bool VCardFormatImpl::load( Addressee &addressee, QFile *file )
{
kdDebug(5700) << "VCardFormat::load()" << endl;
QByteArray fdata = file->readAll();
QCString data(fdata.data(), fdata.size()+1);
VCardEntity e( data );
VCardListIterator it( e.cardList() );
if ( it.current() ) {
VCard v(*it.current());
loadAddressee( addressee, v );
return true;
}
return false;
}
bool VCardFormatImpl::loadAll( AddressBook *addressBook, Resource *resource, QFile *file )
{
kdDebug(5700) << "VCardFormat::loadAll()" << endl;
QByteArray fdata = file->readAll();
QCString data(fdata.data(), fdata.size()+1);
VCardEntity e( data );
VCardListIterator it( e.cardList() );
for (; it.current(); ++it) {
VCard v(*it.current());
Addressee addressee;
loadAddressee( addressee, v );
addressee.setResource( resource );
addressBook->insertAddressee( addressee );
}
return true;
}
void VCardFormatImpl::save( const Addressee &addressee, QFile *file )
{
VCardEntity vcards;
VCardList vcardlist;
vcardlist.setAutoDelete( true );
VCard *v = new VCard;
saveAddressee( addressee, v, false );
vcardlist.append( v );
vcards.setCardList( vcardlist );
QCString vcardData = vcards.asString();
file->writeBlock( (const char*)vcardData, vcardData.length() );
}
void VCardFormatImpl::saveAll( AddressBook *ab, Resource *resource, QFile *file )
{
VCardEntity vcards;
VCardList vcardlist;
vcardlist.setAutoDelete( true );
AddressBook::Iterator it;
for ( it = ab->begin(); it != ab->end(); ++it ) {
if ( (*it).resource() == resource ) {
VCard *v = new VCard;
saveAddressee( (*it), v, false );
(*it).setChanged( false );
vcardlist.append( v );
}
}
vcards.setCardList( vcardlist );
QCString vcardData = vcards.asString();
file->writeBlock( (const char*)vcardData, vcardData.length() );
}
bool VCardFormatImpl::loadAddressee( Addressee& addressee, VCard &v )
{
QPtrList<ContentLine> contentLines = v.contentLineList();
ContentLine *cl;
for( cl = contentLines.first(); cl; cl = contentLines.next() ) {
QCString n = cl->name();
if ( n.left( 2 ) == "X-" ) {
n = n.mid( 2 );
int posDash = n.find( "-" );
addressee.insertCustom( QString::fromUtf8( n.left( posDash ) ),
QString::fromUtf8( n.mid( posDash + 1 ) ),
QString::fromUtf8( cl->value()->asString() ) );
continue;
}
EntityType type = cl->entityType();
switch( type ) {
case EntityUID:
addressee.setUid( readTextValue( cl ) );
break;
case EntityEmail:
addressee.insertEmail( readTextValue( cl ) );
break;
case EntityName:
addressee.setName( readTextValue( cl ) );
break;
case EntityFullName:
addressee.setFormattedName( readTextValue( cl ) );
break;
case EntityURL:
addressee.setUrl( KURL( readTextValue( cl ) ) );
break;
case EntityNickname:
addressee.setNickName( readTextValue( cl ) );
break;
case EntityLabel:
// not yet supported by kabc
break;
case EntityMailer:
addressee.setMailer( readTextValue( cl ) );
break;
case EntityTitle:
addressee.setTitle( readTextValue( cl ) );
break;
case EntityRole:
addressee.setRole( readTextValue( cl ) );
break;
case EntityOrganisation:
addressee.setOrganization( readTextValue( cl ) );
break;
case EntityNote:
addressee.setNote( readTextValue( cl ) );
break;
case EntityProductID:
addressee.setProductId( readTextValue( cl ) );
break;
case EntitySortString:
addressee.setSortString( readTextValue( cl ) );
break;
case EntityN:
readNValue( cl, addressee );
break;
case EntityAddress:
addressee.insertAddress( readAddressValue( cl ) );
break;
case EntityTelephone:
addressee.insertPhoneNumber( readTelephoneValue( cl ) );
break;
case EntityCategories:
addressee.setCategories( QStringList::split( ",", readTextValue( cl ) ) );
break;
case EntityBirthday:
addressee.setBirthday( readDateValue( cl ) );
break;
case EntityRevision:
addressee.setRevision( readDateTimeValue( cl ) );
break;
case EntityGeo:
addressee.setGeo( readGeoValue( cl ) );
break;
case EntityTimeZone:
addressee.setTimeZone( readUTCValue( cl ) );
break;
case EntityVersion:
break;
case EntityClass:
addressee.setSecrecy( readClassValue( cl ) );
break;
case EntityKey:
addressee.insertKey( readKeyValue( cl ) );
break;
case EntityPhoto:
addressee.setPhoto( readPictureValue( cl, EntityPhoto, addressee ) );
break;
case EntityLogo:
addressee.setLogo( readPictureValue( cl, EntityLogo, addressee ) );
break;
case EntityAgent:
addressee.setAgent( readAgentValue( cl ) );
break;
case EntitySound:
addressee.setSound( readSoundValue( cl, addressee ) );
break;
default:
kdDebug(5700) << "VCardFormat::load(): Unsupported entity: "
<< int( type ) << ": " << cl->asString() << endl;
break;
}
}
for( cl = contentLines.first(); cl; cl = contentLines.next() ) {
EntityType type = cl->entityType();
if ( type == EntityLabel ) {
int type = readAddressParam( cl );
Address address = addressee.address( type );
if ( address.isEmpty() )
address.setType( type );
address.setLabel( QString::fromUtf8( cl->value()->asString() ) );
addressee.insertAddress( address );
}
}
return true;
}
void VCardFormatImpl::saveAddressee( const Addressee &addressee, 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 );
}
void VCardFormatImpl::addCustomValue( VCard *v, const QString &txt )
{
if ( txt.isEmpty() ) return;
ContentLine cl;
cl.setName( "X-" + txt.left( txt.find( ":" ) ).utf8() );
QString value = txt.mid( txt.find( ":" ) + 1 );
if ( value.isEmpty() )
return;
cl.setValue( new TextValue( value.utf8() ) );
v->add(cl);
}
void VCardFormatImpl::addTextValue( VCard *v, EntityType type, const QString &txt )
{
if ( txt.isEmpty() ) return;
ContentLine cl;
cl.setName( EntityTypeToParamName( type ) );
cl.setValue( new TextValue( txt.utf8() ) );
v->add(cl);
}
void VCardFormatImpl::addDateValue( VCard *vcard, EntityType type,
const QDate &date )
{
if ( !date.isValid() ) return;
ContentLine cl;
cl.setName( EntityTypeToParamName( type ) );
DateValue *v = new DateValue( date );
cl.setValue( v );
vcard->add(cl);
}
void VCardFormatImpl::addDateTimeValue( VCard *vcard, EntityType type,
const QDateTime &dateTime )
{
if ( !dateTime.isValid() ) return;
ContentLine cl;
cl.setName( EntityTypeToParamName( type ) );
DateValue *v = new DateValue( dateTime );
cl.setValue( v );
vcard->add(cl);
}
void VCardFormatImpl::addAddressValue( VCard *vcard, const Address &a )
{
if ( a.isEmpty() )
return;
ContentLine cl;
cl.setName( EntityTypeToParamName( EntityAddress ) );
AdrValue *v = new AdrValue;
v->setPOBox( a.postOfficeBox().utf8() );
v->setExtAddress( a.extended().utf8() );
v->setStreet( a.street().utf8() );
v->setLocality( a.locality().utf8() );
v->setRegion( a.region().utf8() );
v->setPostCode( a.postalCode().utf8() );
v->setCountryName( a.country().utf8() );
cl.setValue( v );
addAddressParam( &cl, a.type() );
vcard->add( cl );
}
void VCardFormatImpl::addLabelValue( VCard *vcard, const Address &a )
{
if ( a.label().isEmpty() ) return;
ContentLine cl;
cl.setName( EntityTypeToParamName( EntityLabel ) );
cl.setValue( new TextValue( a.label().utf8() ) );
addAddressParam( &cl, a.type() );
vcard->add( cl );
}
void VCardFormatImpl::addAddressParam( ContentLine *cl, int type )
{
ParamList params;
if ( type & Address::Dom ) params.append( new Param( "TYPE", "dom" ) );
if ( type & Address::Intl ) params.append( new Param( "TYPE", "intl" ) );
if ( type & Address::Parcel ) params.append( new Param( "TYPE", "parcel" ) );
if ( type & Address::Postal ) params.append( new Param( "TYPE", "postal" ) );
if ( type & Address::Work ) params.append( new Param( "TYPE", "work" ) );
if ( type & Address::Home ) params.append( new Param( "TYPE", "home" ) );
if ( type & Address::Pref ) params.append( new Param( "TYPE", "pref" ) );
cl->setParamList( params );
}
void VCardFormatImpl::addGeoValue( VCard *vcard, const Geo &geo )
{
if ( !geo.isValid() ) return;
ContentLine cl;
cl.setName( EntityTypeToParamName( EntityGeo ) );
GeoValue *v = new GeoValue;
v->setLatitude( geo.latitude() );
v->setLongitude( geo.longitude() );
cl.setValue( v );
vcard->add(cl);
}
void VCardFormatImpl::addUTCValue( VCard *vcard, const TimeZone &tz )
{
if ( !tz.isValid() ) return;
ContentLine cl;
cl.setName( EntityTypeToParamName( EntityTimeZone ) );
UTCValue *v = new UTCValue;
v->setPositive( tz.offset() >= 0 );
v->setHour( (tz.offset() / 60) * ( tz.offset() >= 0 ? 1 : -1 ) );
v->setMinute( (tz.offset() % 60) * ( tz.offset() >= 0 ? 1 : -1 ) );
cl.setValue( v );
vcard->add(cl);
}
void VCardFormatImpl::addClassValue( VCard *vcard, const Secrecy &secrecy )
{
ContentLine cl;
cl.setName( EntityTypeToParamName( EntityClass ) );
ClassValue *v = new ClassValue;
switch ( secrecy.type() ) {
case Secrecy::Public:
v->setType( (int)ClassValue::Public );
break;
case Secrecy::Private:
v->setType( (int)ClassValue::Private );
break;
case Secrecy::Confidential:
v->setType( (int)ClassValue::Confidential );
break;
}
cl.setValue( v );
vcard->add(cl);
}
Address VCardFormatImpl::readAddressValue( ContentLine *cl )
{
Address a;
AdrValue *v = (AdrValue *)cl->value();
a.setPostOfficeBox( QString::fromUtf8( v->poBox() ) );
a.setExtended( QString::fromUtf8( v->extAddress() ) );
a.setStreet( QString::fromUtf8( v->street() ) );
a.setLocality( QString::fromUtf8( v->locality() ) );
a.setRegion( QString::fromUtf8( v->region() ) );
a.setPostalCode( QString::fromUtf8( v->postCode() ) );
a.setCountry( QString::fromUtf8( v->countryName() ) );
a.setType( readAddressParam( cl ) );
return a;
}
int VCardFormatImpl::readAddressParam( ContentLine *cl )
{
int type = 0;
ParamList params = cl->paramList();
ParamListIterator it( params );
+ QCString tmpStr;
for( ; it.current(); ++it ) {
- if ( (*it)->name() == "TYPE" ) {
- if ( (*it)->value() == "dom" ) type |= Address::Dom;
- else if ( (*it)->value() == "intl" ) type |= Address::Intl;
- else if ( (*it)->value() == "parcel" ) type |= Address::Parcel;
- else if ( (*it)->value() == "postal" ) type |= Address::Postal;
- else if ( (*it)->value() == "work" ) type |= Address::Work;
- else if ( (*it)->value() == "home" ) type |= Address::Home;
- else if ( (*it)->value() == "pref" ) type |= Address::Pref;
+ if ( (*it)->name().upper() == "TYPE" ) {
+ tmpStr = (*it)->value().lower();
+ if ( tmpStr == "dom" ) type |= Address::Dom;
+ else if ( tmpStr == "intl" ) type |= Address::Intl;
+ else if ( tmpStr == "parcel" ) type |= Address::Parcel;
+ else if ( tmpStr == "postal" ) type |= Address::Postal;
+ else if ( tmpStr == "work" ) type |= Address::Work;
+ else if ( tmpStr == "home" ) type |= Address::Home;
+ else if ( tmpStr == "pref" ) type |= Address::Pref;
}
}
return type;
}
void VCardFormatImpl::addNValue( VCard *vcard, const Addressee &a )
{
ContentLine cl;
cl.setName(EntityTypeToParamName( EntityN ) );
NValue *v = new NValue;
v->setFamily( a.familyName().utf8() );
v->setGiven( a.givenName().utf8() );
v->setMiddle( a.additionalName().utf8() );
v->setPrefix( a.prefix().utf8() );
v->setSuffix( a.suffix().utf8() );
cl.setValue( v );
vcard->add(cl);
}
void VCardFormatImpl::readNValue( ContentLine *cl, Addressee &a )
{
NValue *v = (NValue *)cl->value();
a.setFamilyName( QString::fromUtf8( v->family() ) );
a.setGivenName( QString::fromUtf8( v->given() ) );
a.setAdditionalName( QString::fromUtf8( v->middle() ) );
a.setPrefix( QString::fromUtf8( v->prefix() ) );
a.setSuffix( QString::fromUtf8( v->suffix() ) );
}
void VCardFormatImpl::addTelephoneValue( VCard *v, const PhoneNumber &p )
{
if ( p.number().isEmpty() )
return;
ContentLine cl;
cl.setName(EntityTypeToParamName(EntityTelephone));
cl.setValue(new TelValue( p.number().utf8() ));
ParamList params;
if( p.type() & PhoneNumber::Home ) params.append( new Param( "TYPE", "home" ) );
if( p.type() & PhoneNumber::Work ) params.append( new Param( "TYPE", "work" ) );
if( p.type() & PhoneNumber::Msg ) params.append( new Param( "TYPE", "msg" ) );
if( p.type() & PhoneNumber::Pref ) params.append( new Param( "TYPE", "pref" ) );
if( p.type() & PhoneNumber::Voice ) params.append( new Param( "TYPE", "voice" ) );
if( p.type() & PhoneNumber::Fax ) params.append( new Param( "TYPE", "fax" ) );
if( p.type() & PhoneNumber::Cell ) params.append( new Param( "TYPE", "cell" ) );
if( p.type() & PhoneNumber::Video ) params.append( new Param( "TYPE", "video" ) );
if( p.type() & PhoneNumber::Bbs ) params.append( new Param( "TYPE", "bbs" ) );
if( p.type() & PhoneNumber::Modem ) params.append( new Param( "TYPE", "modem" ) );
if( p.type() & PhoneNumber::Car ) params.append( new Param( "TYPE", "car" ) );
if( p.type() & PhoneNumber::Isdn ) params.append( new Param( "TYPE", "isdn" ) );
if( p.type() & PhoneNumber::Pcs ) params.append( new Param( "TYPE", "pcs" ) );
if( p.type() & PhoneNumber::Pager ) params.append( new Param( "TYPE", "pager" ) );
cl.setParamList( params );
v->add(cl);
}
PhoneNumber VCardFormatImpl::readTelephoneValue( ContentLine *cl )
{
PhoneNumber p;
TelValue *value = (TelValue *)cl->value();
p.setNumber( QString::fromUtf8( value->asString() ) );
int type = 0;
ParamList params = cl->paramList();
ParamListIterator it( params );
+ QCString tmpStr;
for( ; it.current(); ++it ) {
if ( (*it)->name() == "TYPE" ) {
- if ( (*it)->value() == "home" ) type |= PhoneNumber::Home;
- else if ( (*it)->value() == "work" ) type |= PhoneNumber::Work;
- else if ( (*it)->value() == "msg" ) type |= PhoneNumber::Msg;
- else if ( (*it)->value() == "pref" ) type |= PhoneNumber::Pref;
- else if ( (*it)->value() == "voice" ) type |= PhoneNumber::Voice;
- else if ( (*it)->value() == "fax" ) type |= PhoneNumber::Fax;
- else if ( (*it)->value() == "cell" ) type |= PhoneNumber::Cell;
- else if ( (*it)->value() == "video" ) type |= PhoneNumber::Video;
- else if ( (*it)->value() == "bbs" ) type |= PhoneNumber::Bbs;
- else if ( (*it)->value() == "modem" ) type |= PhoneNumber::Modem;
- else if ( (*it)->value() == "car" ) type |= PhoneNumber::Car;
- else if ( (*it)->value() == "isdn" ) type |= PhoneNumber::Isdn;
- else if ( (*it)->value() == "pcs" ) type |= PhoneNumber::Pcs;
- else if ( (*it)->value() == "pager" ) type |= PhoneNumber::Pager;
+ tmpStr = (*it)->value().lower();
+ if ( tmpStr == "home" ) type |= PhoneNumber::Home;
+ else if ( tmpStr == "work" ) type |= PhoneNumber::Work;
+ else if ( tmpStr == "msg" ) type |= PhoneNumber::Msg;
+ else if ( tmpStr == "pref" ) type |= PhoneNumber::Pref;
+ else if ( tmpStr == "voice" ) type |= PhoneNumber::Voice;
+ else if ( tmpStr == "fax" ) type |= PhoneNumber::Fax;
+ else if ( tmpStr == "cell" ) type |= PhoneNumber::Cell;
+ else if ( tmpStr == "video" ) type |= PhoneNumber::Video;
+ else if ( tmpStr == "bbs" ) type |= PhoneNumber::Bbs;
+ else if ( tmpStr == "modem" ) type |= PhoneNumber::Modem;
+ else if ( tmpStr == "car" ) type |= PhoneNumber::Car;
+ else if ( tmpStr == "isdn" ) type |= PhoneNumber::Isdn;
+ else if ( tmpStr == "pcs" ) type |= PhoneNumber::Pcs;
+ else if ( tmpStr == "pager" ) type |= PhoneNumber::Pager;
}
}
p.setType( type );
return p;
}
QString VCardFormatImpl::readTextValue( ContentLine *cl )
{
VCARD::Value *value = cl->value();
if ( value ) {
return QString::fromUtf8( value->asString() );
} else {
kdDebug(5700) << "No value: " << cl->asString() << endl;
return QString::null;
}
}
QDate VCardFormatImpl::readDateValue( ContentLine *cl )
{
DateValue *dateValue = (DateValue *)cl->value();
if ( dateValue )
return dateValue->qdate();
else
return QDate();
}
QDateTime VCardFormatImpl::readDateTimeValue( ContentLine *cl )
{
DateValue *dateValue = (DateValue *)cl->value();
if ( dateValue )
return dateValue->qdt();
else
return QDateTime();
}
Geo VCardFormatImpl::readGeoValue( ContentLine *cl )
{
GeoValue *geoValue = (GeoValue *)cl->value();
if ( geoValue ) {
Geo geo( geoValue->latitude(), geoValue->longitude() );
return geo;
} else
return Geo();
}
TimeZone VCardFormatImpl::readUTCValue( ContentLine *cl )
{
UTCValue *utcValue = (UTCValue *)cl->value();
if ( utcValue ) {
TimeZone tz;
tz.setOffset(((utcValue->hour()*60)+utcValue->minute())*(utcValue->positive() ? 1 : -1));
return tz;
} else
return TimeZone();
}
Secrecy VCardFormatImpl::readClassValue( ContentLine *cl )
{
ClassValue *classValue = (ClassValue *)cl->value();
if ( classValue ) {
Secrecy secrecy;
switch ( classValue->type() ) {
case ClassValue::Public:
secrecy.setType( Secrecy::Public );
break;
case ClassValue::Private:
secrecy.setType( Secrecy::Private );
break;
case ClassValue::Confidential:
secrecy.setType( Secrecy::Confidential );
break;
}
return secrecy;
} else
return Secrecy();
}
void VCardFormatImpl::addKeyValue( VCARD::VCard *vcard, const Key &key )
{
ContentLine cl;
cl.setName( EntityTypeToParamName( EntityKey ) );
ParamList params;
if ( key.isBinary() ) {
cl.setValue( new TextValue( KCodecs::base64Encode( key.binaryData() ) ) );
params.append( new Param( "ENCODING", "b" ) );
} else {
cl.setValue( new TextValue( key.textData().utf8() ) );
}
switch ( key.type() ) {
case Key::X509:
params.append( new Param( "TYPE", "X509" ) );
break;
case Key::PGP:
params.append( new Param( "TYPE", "PGP" ) );
break;
case Key::Custom:
params.append( new Param( "TYPE", key.customTypeString().utf8() ) );
break;
}
cl.setParamList( params );
vcard->add( cl );
}
Key VCardFormatImpl::readKeyValue( VCARD::ContentLine *cl )
{
Key key;
bool isBinary = false;
TextValue *v = (TextValue *)cl->value();
ParamList params = cl->paramList();
ParamListIterator it( params );
for( ; it.current(); ++it ) {
if ( (*it)->name() == "ENCODING" && (*it)->value() == "b" )
isBinary = true;
if ( (*it)->name() == "TYPE" ) {
if ( (*it)->value().isEmpty() )
continue;
if ( (*it)->value() == "X509" )
key.setType( Key::X509 );
else if ( (*it)->value() == "PGP" )
key.setType( Key::PGP );
else {
key.setType( Key::Custom );
key.setCustomTypeString( QString::fromUtf8( (*it)->value() ) );
}
}
}
if ( isBinary ) {
QByteArray data;
KCodecs::base64Decode( v->asString().stripWhiteSpace(), data );
key.setBinaryData( data );
} else {
key.setTextData( QString::fromUtf8( v->asString() ) );
}
return key;
}
void VCardFormatImpl::addAgentValue( VCARD::VCard *vcard, const Agent &agent )
{
if ( agent.isIntern() && !agent.addressee() )
return;
if ( !agent.isIntern() && agent.url().isEmpty() )
return;
ContentLine cl;
cl.setName( EntityTypeToParamName( EntityAgent ) );
ParamList params;
if ( agent.isIntern() ) {
QString vstr;
Addressee *addr = agent.addressee();
if ( addr ) {
writeToString( (*addr), vstr );
qDebug("VCardFormatImpl::addAgentValue please verify if replace is correct");
/*US
vstr.replace( ":", "\\:" );
vstr.replace( ",", "\\," );
vstr.replace( ";", "\\;" );
vstr.replace( "\r\n", "\\n" );
*/
vstr.replace( QRegExp(":"), "\\:" );
vstr.replace( QRegExp(","), "\\," );
vstr.replace( QRegExp(";"), "\\;" );
vstr.replace( QRegExp("\r\n"), "\\n" );
cl.setValue( new TextValue( vstr.utf8() ) );
} else
return;
} else {
cl.setValue( new TextValue( agent.url().utf8() ) );
params.append( new Param( "VALUE", "uri" ) );
}
cl.setParamList( params );
vcard->add( cl );
}
Agent VCardFormatImpl::readAgentValue( VCARD::ContentLine *cl )
{
Agent agent;
bool isIntern = true;
TextValue *v = (TextValue *)cl->value();
ParamList params = cl->paramList();
ParamListIterator it( params );
for( ; it.current(); ++it ) {
if ( (*it)->name() == "VALUE" && (*it)->value() == "uri" )
isIntern = false;
}
if ( isIntern ) {
QString vstr = QString::fromUtf8( v->asString() );
qDebug("VCardFormatImpl::addAgentValue please verify if replace is correct");
/*US
vstr.replace( "\\n", "\r\n" );
vstr.replace( "\\:", ":" );
vstr.replace( "\\,", "," );
vstr.replace( "\\;", ";" );
*/
vstr.replace( QRegExp("\\n"), "\r\n" );
vstr.replace( QRegExp("\\:"), ":" );
vstr.replace( QRegExp("\\,"), "," );
vstr.replace( QRegExp("\\;"), ";" );
Addressee *addr = new Addressee;
readFromString( vstr, *addr );
agent.setAddressee( addr );
} else {
agent.setUrl( QString::fromUtf8( v->asString() ) );
}
return agent;
}
void VCardFormatImpl::addPictureValue( VCARD::VCard *vcard, VCARD::EntityType type, const Picture &pic, const Addressee &addr, bool intern )
{
ContentLine cl;
cl.setName( EntityTypeToParamName( type ) );
if ( pic.isIntern() && pic.data().isNull() )
return;
if ( !pic.isIntern() && pic.url().isEmpty() )
return;
ParamList params;
if ( pic.isIntern() ) {
QImage img = pic.data();
if ( intern ) { // only for vCard export we really write the data inline
QByteArray data;
QDataStream s( data, IO_WriteOnly );
s.setVersion( 4 ); // to produce valid png files
s << img;
cl.setValue( new TextValue( KCodecs::base64Encode( data ) ) );
} else { // save picture in cache
QString dir;
if ( type == EntityPhoto )
dir = "photos";
if ( type == EntityLogo )
dir = "logos";
img.save( locateLocal( "data", "kabc/" + dir + "/" + addr.uid() ), pic.type().utf8() );
cl.setValue( new TextValue( "<dummy>" ) );
}
params.append( new Param( "ENCODING", "b" ) );
if ( !pic.type().isEmpty() )
params.append( new Param( "TYPE", pic.type().utf8() ) );
} else {
cl.setValue( new TextValue( pic.url().utf8() ) );
params.append( new Param( "VALUE", "uri" ) );
}
cl.setParamList( params );
vcard->add( cl );
}
Picture VCardFormatImpl::readPictureValue( VCARD::ContentLine *cl, VCARD::EntityType type, const Addressee &addr )
{
Picture pic;
bool isInline = false;
QString picType;
TextValue *v = (TextValue *)cl->value();
ParamList params = cl->paramList();
ParamListIterator it( params );
for( ; it.current(); ++it ) {
if ( (*it)->name() == "ENCODING" && (*it)->value() == "b" )
isInline = true;
if ( (*it)->name() == "TYPE" && !(*it)->value().isEmpty() )
picType = QString::fromUtf8( (*it)->value() );
}
if ( isInline ) {
QImage img;
if ( v->asString() == "<dummy>" ) { // no picture inline stored => picture is in cache
QString dir;
if ( type == EntityPhoto )
dir = "photos";
if ( type == EntityLogo )
dir = "logos";
img.load( locateLocal( "data", "kabc/" + dir + "/" + addr.uid() ) );
} else {
QByteArray data;
KCodecs::base64Decode( v->asString(), data );
img.loadFromData( data );
}
pic.setData( img );
pic.setType( picType );
} else {
pic.setUrl( QString::fromUtf8( v->asString() ) );
}
return pic;
}
void VCardFormatImpl::addSoundValue( VCARD::VCard *vcard, const Sound &sound, const Addressee &addr, bool intern )
{
ContentLine cl;
cl.setName( EntityTypeToParamName( EntitySound ) );
if ( sound.isIntern() && sound.data().isNull() )
return;
if ( !sound.isIntern() && sound.url().isEmpty() )
return;
ParamList params;
if ( sound.isIntern() ) {
QByteArray data = sound.data();
if ( intern ) { // only for vCard export we really write the data inline
cl.setValue( new TextValue( KCodecs::base64Encode( data ) ) );
} else { // save sound in cache
QFile file( locateLocal( "data", "kabc/sounds/" + addr.uid() ) );
if ( file.open( IO_WriteOnly ) ) {
file.writeBlock( data );
}
cl.setValue( new TextValue( "<dummy>" ) );
}
params.append( new Param( "ENCODING", "b" ) );
} else {
cl.setValue( new TextValue( sound.url().utf8() ) );
params.append( new Param( "VALUE", "uri" ) );
}
cl.setParamList( params );
vcard->add( cl );
}
Sound VCardFormatImpl::readSoundValue( VCARD::ContentLine *cl, const Addressee &addr )
{
Sound sound;
bool isInline = false;
TextValue *v = (TextValue *)cl->value();
ParamList params = cl->paramList();
ParamListIterator it( params );
for( ; it.current(); ++it ) {
if ( (*it)->name() == "ENCODING" && (*it)->value() == "b" )
isInline = true;
}
if ( isInline ) {
QByteArray data;
if ( v->asString() == "<dummy>" ) { // no sound inline stored => sound is in cache
QFile file( locateLocal( "data", "kabc/sounds/" + addr.uid() ) );
if ( file.open( IO_ReadOnly ) ) {
data = file.readAll();
file.close();
}
} else {
KCodecs::base64Decode( v->asString(), data );
}
sound.setData( data );
} else {
sound.setUrl( QString::fromUtf8( v->asString() ) );
}
return sound;
}
bool VCardFormatImpl::readFromString( const QString &vcard, Addressee &addressee )
{
VCardEntity e( vcard.utf8() );
VCardListIterator it( e.cardList() );
if ( it.current() ) {
VCard v(*it.current());
loadAddressee( addressee, v );
return true;
}
return false;
}
bool VCardFormatImpl::writeToString( const Addressee &addressee, QString &vcard )
{
VCardEntity vcards;
VCardList vcardlist;
vcardlist.setAutoDelete( true );
VCard *v = new VCard;
saveAddressee( addressee, v, true );
vcardlist.append( v );
vcards.setCardList( vcardlist );
vcard = QString::fromUtf8( vcards.asString() );
return true;
}
diff --git a/kabc/vcardformatplugin.cpp b/kabc/vcardformatplugin.cpp
index 3cba59a..bc18690 100644
--- a/kabc/vcardformatplugin.cpp
+++ b/kabc/vcardformatplugin.cpp
@@ -1,66 +1,70 @@
/*
This file is part of libkabc.
Copyright (c) 2001 Cornelius Schumacher <schumacher@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 "vcardformatplugin.h"
#include "vcardformatimpl.h"
using namespace KABC;
VCardFormatPlugin::VCardFormatPlugin()
{
mImpl = new VCardFormatImpl;
}
VCardFormatPlugin::~VCardFormatPlugin()
{
delete mImpl;
}
bool VCardFormatPlugin::load( Addressee &addressee, QFile *file )
{
+ qDebug("VCardFormatPlugin::load");
return mImpl->load( addressee, file );
}
bool VCardFormatPlugin::loadAll( AddressBook *addressBook, Resource *resource, QFile *file )
{
+ qDebug("VCardFormatPlugin::loadAll");
return mImpl->loadAll( addressBook, resource, file );
}
void VCardFormatPlugin::save( const Addressee &addressee, QFile *file )
{
+ qDebug("VCardFormatPlugin::save");
mImpl->save( addressee, file );
}
void VCardFormatPlugin::saveAll( AddressBook *addressBook, Resource *resource, QFile *file )
{
+ qDebug("VCardFormatPlugin::saveAll");
mImpl->saveAll( addressBook, resource, file );
}
bool VCardFormatPlugin::checkFormat( QFile *file ) const
{
QString line;
file->readLine( line, 1024 );
line = line.stripWhiteSpace();
if ( line == "BEGIN:VCARD" )
return true;
else
return false;
}
diff --git a/kabc/vcardparser/vcardtool.cpp b/kabc/vcardparser/vcardtool.cpp
index 01c5b3e..71f29d7 100644
--- a/kabc/vcardparser/vcardtool.cpp
+++ b/kabc/vcardparser/vcardtool.cpp
@@ -1,883 +1,897 @@
/*
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 <qdatastream.h>
#include <qstring.h>
#include <qregexp.h>
#include <kmdcodec.h>
#include "agent.h"
#include "key.h"
#include "picture.h"
#include "secrecy.h"
#include "sound.h"
#include "vcardtool.h"
using namespace KABC;
VCardTool::VCardTool()
{
mAddressTypeMap.insert( "dom", Address::Dom );
mAddressTypeMap.insert( "intl", Address::Intl );
mAddressTypeMap.insert( "postal", Address::Postal );
mAddressTypeMap.insert( "parcel", Address::Parcel );
mAddressTypeMap.insert( "home", Address::Home );
mAddressTypeMap.insert( "work", Address::Work );
mAddressTypeMap.insert( "pref", Address::Pref );
mPhoneTypeMap.insert( "HOME", PhoneNumber::Home );
mPhoneTypeMap.insert( "WORK", PhoneNumber::Work );
mPhoneTypeMap.insert( "MSG", PhoneNumber::Msg );
mPhoneTypeMap.insert( "PREF", PhoneNumber::Pref );
mPhoneTypeMap.insert( "VOICE", PhoneNumber::Voice );
mPhoneTypeMap.insert( "FAX", PhoneNumber::Fax );
mPhoneTypeMap.insert( "CELL", PhoneNumber::Cell );
mPhoneTypeMap.insert( "VIDEO", PhoneNumber::Video );
mPhoneTypeMap.insert( "BBS", PhoneNumber::Bbs );
mPhoneTypeMap.insert( "MODEM", PhoneNumber::Modem );
mPhoneTypeMap.insert( "CAR", PhoneNumber::Car );
mPhoneTypeMap.insert( "ISDN", PhoneNumber::Isdn );
mPhoneTypeMap.insert( "PCS", PhoneNumber::Pcs );
mPhoneTypeMap.insert( "PAGER", PhoneNumber::Pager );
}
VCardTool::~VCardTool()
{
}
QString VCardTool::createVCards( Addressee::List list, VCard::Version version )
{
VCard::List vCardList;
Addressee::List::Iterator addrIt;
for ( addrIt = list.begin(); addrIt != list.end(); ++addrIt ) {
VCard card;
QStringList::ConstIterator strIt;
// ADR + LABEL
Address::List addresses = (*addrIt).addresses();
for ( Address::List::Iterator it = addresses.begin(); it != addresses.end(); ++it ) {
QStringList address;
/*US
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( ';', "\\;" ) );
*/
//US using the old implementation instead
//qDebug("VCardTool::createVCards has to be verified");
address.append( (*it).postOfficeBox().replace( QRegExp(";"), "\\;" ) );
address.append( (*it).extended().replace( QRegExp(";"), "\\;" ) );
address.append( (*it).street().replace( QRegExp(";"), "\\;" ) );
address.append( (*it).locality().replace( QRegExp(";"), "\\;" ) );
address.append( (*it).region().replace( QRegExp(";"), "\\;" ) );
address.append( (*it).postalCode().replace( QRegExp(";"), "\\;" ) );
address.append( (*it).country().replace( QRegExp(";"), "\\;" ) );
VCardLine adrLine( "ADR", address.join( ";" ) );
VCardLine labelLine( "LABEL", (*it).label() );
bool hasLabel = !(*it).label().isEmpty();
QMap<QString, int>::Iterator typeIt;
for ( typeIt = mAddressTypeMap.begin(); typeIt != mAddressTypeMap.end(); ++typeIt ) {
if ( typeIt.data() & (*it).type() ) {
- adrLine.addParameter( "TYPE", typeIt.key() );
- if ( hasLabel )
- labelLine.addParameter( "TYPE", typeIt.key() );
+ if ( version == VCard::v3_0 ) {
+ adrLine.addParameter( "TYPE", typeIt.key().lower() );
+ }
+ else {
+ adrLine.addParameter( "TYPE", typeIt.key() );
+ }
+ if ( hasLabel ) {
+ if ( version == VCard::v3_0 ) {
+ labelLine.addParameter( "TYPE", typeIt.key().lower() );
+ }
+ else {
+ labelLine.addParameter( "TYPE", typeIt.key() );
+ }
+ }
}
}
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 )
{
//US using the old implementation instead
// qDebug("VCardTool::createVCards has to be verified");
//US (*catIt).replace( ',', "\\," );
(*catIt).replace( QRegExp(","), "\\," );
}
card.addLine( VCardLine( "CATEGORIES", categories.join( "," ) ) );
}
// CLASS
if ( version == VCard::v3_0 ) {
card.addLine( createSecrecy( (*addrIt).secrecy() ) );
}
// EMAIL
QStringList emails = (*addrIt).emails();
bool pref = true;
for ( strIt = emails.begin(); strIt != emails.end(); ++strIt ) {
VCardLine line( "EMAIL", *strIt );
if ( pref == true ) {
line.addParameter( "TYPE", "PREF" );
pref = false;
}
card.addLine( line );
}
// FN
card.addLine( VCardLine( "FN", (*addrIt).formattedName() ) );
// GEO
Geo geo = (*addrIt).geo();
if ( geo.isValid() ) {
QString str;
str.sprintf( "%.6f;%.6f", geo.latitude(), geo.longitude() );
card.addLine( VCardLine( "GEO", str ) );
}
// KEY
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
card.addLine( VCardLine( "MAILER", (*addrIt).mailer() ) );
// N
QStringList name;
//US using the old implementation instead
//qDebug("VCardTool::createVCards has to be verified");
/*US
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( ';', "\\;" ) );
*/
name.append( (*addrIt).familyName().replace( QRegExp(";"), "\\;" ) );
name.append( (*addrIt).givenName().replace( QRegExp(";"), "\\;" ) );
name.append( (*addrIt).additionalName().replace( QRegExp(";"), "\\;" ) );
name.append( (*addrIt).prefix().replace( QRegExp(";"), "\\;" ) );
name.append( (*addrIt).suffix().replace( QRegExp(";"), "\\;" ) );
if ( !name.join( "" ).isEmpty() )
card.addLine( VCardLine( "N", name.join( ";" ) ) );
// NICKNAME
if ( version == VCard::v3_0 )
card.addLine( VCardLine( "NICKNAME", (*addrIt).nickName() ) );
// NOTE
card.addLine( VCardLine( "NOTE", (*addrIt).note() ) );
// ORG
card.addLine( VCardLine( "ORG", (*addrIt).organization() ) );
// 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
card.addLine( VCardLine( "ROLE", (*addrIt).role() ) );
// SORT-STRING
if ( version == VCard::v3_0 )
card.addLine( VCardLine( "SORT-STRING", (*addrIt).sortString() ) );
// SOUND
card.addLine( createSound( (*addrIt).sound() ) );
// TEL
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>::Iterator typeIt;
for ( typeIt = mPhoneTypeMap.begin(); typeIt != mPhoneTypeMap.end(); ++typeIt ) {
if ( typeIt.data() & (*phoneIt).type() )
- line.addParameter( "TYPE", typeIt.key() );
+ if ( version == VCard::v3_0 )
+ line.addParameter( "TYPE", typeIt.key().lower() );
+ else
+ line.addParameter( "TYPE", typeIt.key() );
}
card.addLine( line );
}
// TITLE
card.addLine( VCardLine( "TITLE", (*addrIt).title() ) );
// 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-
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;
card.addLine( VCardLine( identifier, value ) );
}
vCardList.append( card );
}
return VCardParser::createVCards( vCardList );
}
Addressee::List VCardTool::parseVCards( const QString& vcard )
{
QChar semicolonSep( ';' );
QChar commaSep( ',' );
QString identifier;
Addressee::List addrList;
VCard::List vCardList = VCardParser::parseVCards( vcard );
VCard::List::Iterator cardIt;
for ( cardIt = vCardList.begin(); cardIt != vCardList.end(); ++cardIt ) {
Addressee addr;
QStringList idents = (*cardIt).identifiers();
QStringList::ConstIterator identIt;
for ( identIt = idents.begin(); identIt != idents.end(); ++identIt ) {
VCard card = (*cardIt);
VCardLine::List lines = card.lines( (*identIt) );
VCardLine::List::Iterator lineIt;
// iterate over the lines
for ( lineIt = lines.begin(); lineIt != lines.end(); ++lineIt ) {
QStringList params = (*lineIt).parameterList();
identifier = (*lineIt).identifier().lower();
// ADR
if ( identifier == "adr" ) {
Address address;
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 ].replace ( QRegExp("\\\\n") , "\n") );
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;
QStringList types = (*lineIt).parameters( "type" );
for ( QStringList::Iterator it = types.begin(); it != types.end(); ++it )
type += mAddressTypeMap[ (*it).lower() ];
if ( !type )
type = Address::Home; // default
address.setType( type );
addr.insertAddress( address );
}
// AGENT
if ( identifier == "agent" )
addr.setAgent( parseAgent( *lineIt ) );
// BDAY
if ( identifier == "bday" )
addr.setBirthday( parseDateTime( (*lineIt).value().asString() ) );
// CATEGORIES
if ( identifier == "categories" ) {
QStringList categories = splitString( commaSep, (*lineIt).value().asString() );
addr.setCategories( categories );
}
// CLASS
if ( identifier == "class" )
addr.setSecrecy( parseSecrecy( *lineIt ) );
// EMAIL
if ( identifier == "email" ) {
QStringList types = (*lineIt).parameters( "type" );
addr.insertEmail( (*lineIt).value().asString(), types.contains( "PREF" ) );
}
// FN
if ( identifier == "fn" )
addr.setFormattedName( (*lineIt).value().asString() );
// GEO
if ( identifier == "geo" ) {
Geo geo;
QStringList geoParts = QStringList::split( ';', (*lineIt).value().asString(), true );
geo.setLatitude( geoParts[ 0 ].toFloat() );
geo.setLongitude( geoParts[ 1 ].toFloat() );
addr.setGeo( geo );
}
// KEY
if ( identifier == "key" )
addr.insertKey( parseKey( *lineIt ) );
// LABEL
if ( identifier == "label" ) {
int type = 0;
QStringList types = (*lineIt).parameters( "type" );
for ( QStringList::Iterator it = types.begin(); it != types.end(); ++it )
type += mAddressTypeMap[ (*it).lower() ];
if ( !type )
type = Address::Home;
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 );
}
}
}
// LOGO
if ( identifier == "logo" )
addr.setLogo( parsePicture( *lineIt ) );
// MAILER
if ( identifier == "mailer" )
addr.setMailer( (*lineIt).value().asString() );
// N
if ( identifier == "n" ) {
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 ] );
}
// NICKNAME
if ( identifier == "nickname" )
addr.setNickName( (*lineIt).value().asString() );
// NOTE
if ( identifier == "note" ) {
// #ifdef DESKTOP_VERSION
// addr.setNote( (*lineIt).value().asString() );
// #else
QString note = (*lineIt).value().asString();
if ( ! note.isEmpty() )
addr.setNote( note.replace ( QRegExp("\\\\n") , "\n") );
else
addr.setNote( note );
//#endif
}
// ORGANIZATION
if ( identifier == "org" )
addr.setOrganization( (*lineIt).value().asString() );
// PHOTO
if ( identifier == "photo" )
addr.setPhoto( parsePicture( *lineIt ) );
// PROID
if ( identifier == "prodid" )
addr.setProductId( (*lineIt).value().asString() );
// REV
if ( identifier == "rev" )
addr.setRevision( parseDateTime( (*lineIt).value().asString() ) );
// ROLE
if ( identifier == "role" )
addr.setRole( (*lineIt).value().asString() );
// SORT-STRING
if ( identifier == "sort-string" )
addr.setSortString( (*lineIt).value().asString() );
// SOUND
if ( identifier == "sound" )
addr.setSound( parseSound( *lineIt ) );
// TEL
if ( identifier == "tel" ) {
PhoneNumber phone;
phone.setNumber( (*lineIt).value().asString() );
int type = 0;
QStringList types = (*lineIt).parameters( "type" );
for ( QStringList::Iterator it = types.begin(); it != types.end(); ++it )
type += mPhoneTypeMap[(*it).upper()];
if ( !type )
type = PhoneNumber::Home; // default
phone.setType( type );
addr.insertPhoneNumber( phone );
}
// TITLE
if ( identifier == "title" )
addr.setTitle( (*lineIt).value().asString() );
// TZ
if ( identifier == "tz" ) {
TimeZone tz;
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
if ( identifier == "uid" )
addr.setUid( (*lineIt).value().asString() );
// URL
if ( identifier == "url" )
addr.setUrl( (*lineIt).value().asString() );
// X-
if ( identifier.startsWith( "x-" ) ) {
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;
}
QDateTime VCardTool::parseDateTime( const QString &str )
{
QDateTime dateTime;
if ( str.find( '-' ) == -1 ) { // is base format (yyyymmdd)
dateTime.setDate( QDate( str.left( 4 ).toInt(), str.mid( 4, 2 ).toInt(),
str.mid( 6, 2 ).toInt() ) );
if ( str.find( 'T' ) ) // has time information yyyymmddThh:mm:ss
dateTime.setTime( QTime( str.mid( 11, 2 ).toInt(), str.mid( 14, 2 ).toInt(),
str.mid( 17, 2 ).toInt() ) );
} else { // is extended format yyyy-mm-dd
dateTime.setDate( QDate( str.left( 4 ).toInt(), str.mid( 5, 2 ).toInt(),
str.mid( 8, 2 ).toInt() ) );
if ( str.find( 'T' ) ) // has time information yyyy-mm-ddThh:mm:ss
dateTime.setTime( QTime( str.mid( 11, 2 ).toInt(), str.mid( 14, 2 ).toInt(),
str.mid( 17, 2 ).toInt() ) );
}
return dateTime;
}
QString VCardTool::createDateTime( const QDateTime &dateTime )
{
QString str;
if ( dateTime.date().isValid() ) {
str.sprintf( "%4d-%02d-%02d", dateTime.date().year(), dateTime.date().month(),
dateTime.date().day() );
if ( dateTime.time().isValid() ) {
QString tmp;
tmp.sprintf( "T%02d:%02d:%02dZ", dateTime.time().hour(), dateTime.time().minute(),
dateTime.time().second() );
str += tmp;
}
}
return str;
}
Picture VCardTool::parsePicture( const VCardLine &line )
{
Picture pic;
QStringList params = line.parameterList();
if ( params.contains( "encoding" ) ) {
QCString cs(line.value().asCString());
QByteArray input, output;
input = line.value().asCString();
if ( line.parameter( "encoding" ).lower() == "b" )
KCodecs::base64Decode( input, output );
else if ( line.parameter( "encoding" ).lower() == "quoted-printable" )
KCodecs::quotedPrintableDecode( input, output );
qDebug("********** DECODING OKAY ************** (picture)");
pic.setData( QImage(output) );
}
else if ( params.contains( "value" ) ) {
if ( line.parameter( "value" ).lower() == "uri" )
pic.setUrl( line.value().asString() );
}
if ( params.contains( "type" ) )
pic.setType( line.parameter( "type" ) );
return pic;
}
VCardLine VCardTool::createPicture( const QString &identifier, const Picture &pic )
{
// LR fixed
VCardLine line( identifier );
if ( pic.isIntern() ) {
if ( !pic.data().isNull() ) {
#if 0
QByteArray input;
QDataStream s( input, IO_WriteOnly );
s.setVersion( 4 );
s << pic.data();
line.setValue( input );
#else
QCString input;
QDataStream s( input, IO_WriteOnly );
s.setVersion( 4 );
s << pic.data();
//QCString cs(line.value().asCString());
//QImage qi(cs);
line.setValue( input );
#endif
line.addParameter( "encoding", "b" );
line.addParameter( "type", "image/png" );
}
} else if ( !pic.url().isEmpty() ) {
line.setValue( pic.url() );
line.addParameter( "value", "URI" );
}
return line;
}
Sound VCardTool::parseSound( const VCardLine &line )
{
Sound snd;
QStringList params = line.parameterList();
if ( params.contains( "encoding" ) ) {
qDebug("VCardTool::parseSound has to be verified");
//US snd.setData( line.value().asByteArray() );
//US I am not sure if this is correct
QCString cs(line.value().asCString());
snd.setData( cs );
}
else if ( params.contains( "value" ) ) {
if ( line.parameter( "value" ).lower() == "uri" )
snd.setUrl( line.value().asString() );
}
/* TODO: support sound types
if ( params.contains( "type" ) )
snd.setType( line.parameter( "type" ) );
*/
return snd;
}
VCardLine VCardTool::createSound( const Sound &snd )
{
VCardLine line( "SOUND" );
if ( snd.isIntern() ) {
if ( !snd.data().isEmpty() ) {
qDebug("VCardTool::createSound has to be verified");
//US line.setValue( snd.data() );
//US I am not sure if this is correct
QCString cs(snd.data());
line.setValue( cs );
line.addParameter( "encoding", "b" );
// TODO: need to store sound type!!!
}
} else if ( !snd.url().isEmpty() ) {
line.setValue( snd.url() );
line.addParameter( "value", "URI" );
}
return line;
}
Key VCardTool::parseKey( const VCardLine &line )
{
Key key;
QStringList params = line.parameterList();
if ( params.contains( "encoding" ) ) {
qDebug("VCardTool::parseKey has to be verified");
//US key.setBinaryData( line.value().asByteArray() );
//US I am not sure if this is correct
QCString cs( line.value().asCString() );
key.setBinaryData( cs );
}
else
key.setTextData( line.value().asString() );
if ( params.contains( "type" ) ) {
if ( line.parameter( "type" ).lower() == "x509" )
key.setType( Key::X509 );
else if ( line.parameter( "type" ).lower() == "pgp" )
key.setType( Key::PGP );
else {
key.setType( Key::Custom );
key.setCustomTypeString( line.parameter( "type" ) );
}
}
return key;
}
VCardLine VCardTool::createKey( const Key &key )
{
VCardLine line( "KEY" );
if ( key.isBinary() ) {
if ( !key.binaryData().isEmpty() ) {
qDebug("VCardTool::createKey has to be verified");
//US line.setValue( key.binaryData() );
//US I am not sure if this is correct
QCString cs(key.binaryData());
line.setValue( cs );
line.addParameter( "encoding", "b" );
}
} else if ( !key.textData().isEmpty() )
line.setValue( key.textData() );
if ( key.type() == Key::X509 )
line.addParameter( "type", "X509" );
else if ( key.type() == Key::PGP )
line.addParameter( "type", "PGP" );
else if ( key.type() == Key::Custom )
line.addParameter( "type", key.customTypeString() );
return line;
}
Secrecy VCardTool::parseSecrecy( const VCardLine &line )
{
Secrecy secrecy;
if ( line.value().asString().lower() == "public" )
secrecy.setType( Secrecy::Public );
if ( line.value().asString().lower() == "private" )
secrecy.setType( Secrecy::Private );
if ( line.value().asString().lower() == "confidential" )
secrecy.setType( Secrecy::Confidential );
return secrecy;
}
VCardLine VCardTool::createSecrecy( const Secrecy &secrecy )
{
VCardLine line( "CLASS" );
int type = secrecy.type();
if ( type == Secrecy::Public )
line.setValue( "PUBLIC" );
else if ( type == Secrecy::Private )
line.setValue( "PRIVATE" );
else if ( type == Secrecy::Confidential )
line.setValue( "CONFIDENTIAL" );
return line;
}
Agent VCardTool::parseAgent( const VCardLine &line )
{
Agent agent;
QStringList params = line.parameterList();
if ( params.contains( "value" ) ) {
if ( line.parameter( "value" ).lower() == "uri" )
agent.setUrl( line.value().asString() );
} else {
QString str = line.value().asString();
//US using the old implementation instead
qDebug("VCardTool::parseAgent has to be verified");
/*US
str.replace( "\\n", "\r\n" );
str.replace( "\\N", "\r\n" );
str.replace( "\\;", ";" );
str.replace( "\\:", ":" );
str.replace( "\\,", "," );
*/
str.replace( QRegExp("\\n") , "\r\n" );
str.replace( QRegExp("\\N") , "\r\n" );
str.replace( QRegExp("\\;") , ";" );
str.replace( QRegExp("\\:") , ":" );
str.replace( QRegExp("\\,") , "," );
Addressee::List list = parseVCards( str );
if ( list.count() > 0 ) {
Addressee *addr = new Addressee;
*addr = list[ 0 ];
agent.setAddressee( addr );
}
}
return agent;
}
VCardLine VCardTool::createAgent( VCard::Version version, const Agent &agent )
{
VCardLine line( "AGENT" );
if ( agent.isIntern() ) {
if ( agent.addressee() != 0 ) {
Addressee::List list;
list.append( *agent.addressee() );
QString str = createVCards( list, version );
//US using the old implementation instead
qDebug("VCardTool::createAgent has to be verified");
/*US
str.replace( "\r\n", "\\n" );
str.replace( ";", "\\;" );
str.replace( ":", "\\:" );
str.replace( ",", "\\," );
*/
str.replace( QRegExp("\r\n"), "\\n" );
str.replace( QRegExp(";"), "\\;" );
str.replace( QRegExp(":"), "\\:" );
str.replace( QRegExp(","), "\\," );
line.setValue( str );
}
} else if ( !agent.url().isEmpty() ) {
line.setValue( agent.url() );
line.addParameter( "value", "URI" );
}
return line;
}
QStringList VCardTool::splitString( const QChar &sep, const QString &str )
{
QStringList list;
QString value( str );
int start = 0;
int pos = value.find( sep, start );
while ( pos != -1 ) {
if ( value[ pos - 1 ] != '\\' ) {
if ( pos > start && pos <= (int)value.length() )
list << value.mid( start, pos - start );
else
list << QString::null;
start = pos + 1;
pos = value.find( sep, start );
} else {
if ( pos != 0 ) {
value.replace( pos - 1, 2, sep );
pos = value.find( sep, pos );
} else
pos = value.find( sep, pos + 1 );
}
}
int l = value.length() - 1;
if ( value.mid( start, l - start + 1 ).length() > 0 )
list << value.mid( start, l - start + 1 );
else
list << QString::null;
return list;
}