00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "vcarddrag.h"
00023
00024 #include "vcardconverter.h"
00025
00026 #include <KMimeType>
00027
00028 #include <QMimeData>
00029
00030 using namespace KABC;
00031
00032 static QString findCompatibleMimeType( const QMimeData *md )
00033 {
00034
00035 if ( md->hasFormat( KABC::Addressee::mimeType() ) ) {
00036 return KABC::Addressee::mimeType();
00037 }
00038
00039 const QStringList mimeTypeOffers = md->formats();
00040 Q_FOREACH( const QString &mimeType, mimeTypeOffers ) {
00041 const KMimeType::Ptr mimeTypePtr = KMimeType::mimeType( mimeType, KMimeType::ResolveAliases );
00042 if ( !mimeTypePtr.isNull() ) {
00043 if ( mimeTypePtr->is( KABC::Addressee::mimeType() ) ) {
00044 return mimeType;
00045 }
00046 }
00047 }
00048
00049 return QString();
00050 }
00051
00052 bool VCardDrag::populateMimeData( QMimeData *md, const QByteArray &content )
00053 {
00054 md->setData( KABC::Addressee::mimeType(), content );
00055 return true;
00056 }
00057
00058 bool VCardDrag::populateMimeData( QMimeData *md, const KABC::Addressee::List &addressees )
00059 {
00060 KABC::VCardConverter converter;
00061 const QByteArray vcards = converter.createVCards( addressees );
00062 if ( !vcards.isEmpty() ) {
00063 return populateMimeData( md, vcards );
00064 } else {
00065 return false;
00066 }
00067 }
00068
00069 bool VCardDrag::canDecode( const QMimeData *md )
00070 {
00071 return !findCompatibleMimeType( md ).isEmpty();
00072 }
00073
00074 bool VCardDrag::fromMimeData( const QMimeData *md, QByteArray &content )
00075 {
00076 const QString mimeOffer = findCompatibleMimeType( md );
00077 if ( mimeOffer.isEmpty() ) {
00078 return false;
00079 }
00080 content = md->data( mimeOffer );
00081 return !content.isEmpty();
00082 }
00083
00084 bool VCardDrag::fromMimeData( const QMimeData *md, KABC::Addressee::List &addressees )
00085 {
00086 const QString mimeOffer = findCompatibleMimeType( md );
00087 if ( mimeOffer.isEmpty() ) {
00088 return false;
00089 }
00090 addressees = KABC::VCardConverter().parseVCards( md->data( mimeOffer ) );
00091 return !addressees.isEmpty();
00092 }
00093
00094