1/*
2 This file is part of libkabc.
3
4 Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20*/
21
22#include "vcarddrag.h"
23
24#include "vcardconverter.h"
25
26#include <KMimeType>
27
28#include <QMimeData>
29
30using namespace KABC;
31
32static QString findCompatibleMimeType( const QMimeData *md )
33{
34 // check the canonical MIME type first
35 if ( md->hasFormat( KABC::Addressee::mimeType() ) ) {
36 return KABC::Addressee::mimeType();
37 }
38
39 const QStringList mimeTypeOffers = md->formats();
40 Q_FOREACH ( const QString &mimeType, mimeTypeOffers ) {
41 const KMimeType::Ptr mimeTypePtr = KMimeType::mimeType( mimeType, KMimeType::ResolveAliases );
42 if ( !mimeTypePtr.isNull() ) {
43 if ( mimeTypePtr->is( KABC::Addressee::mimeType() ) ) {
44 return mimeType;
45 }
46 }
47 }
48
49 return QString();
50}
51
52bool VCardDrag::populateMimeData( QMimeData *md, const QByteArray &content )
53{
54 md->setData( KABC::Addressee::mimeType(), content );
55 return true;
56}
57
58bool VCardDrag::populateMimeData( QMimeData *md, const KABC::Addressee::List &addressees )
59{
60 KABC::VCardConverter converter;
61 const QByteArray vcards = converter.createVCards( addressees );
62 if ( !vcards.isEmpty() ) {
63 return populateMimeData( md, vcards );
64 } else {
65 return false;
66 }
67}
68
69bool VCardDrag::canDecode( const QMimeData *md )
70{
71 return !findCompatibleMimeType( md ).isEmpty();
72}
73
74bool VCardDrag::fromMimeData( const QMimeData *md, QByteArray &content )
75{
76 const QString mimeOffer = findCompatibleMimeType( md );
77 if ( mimeOffer.isEmpty() ) {
78 return false;
79 }
80 content = md->data( mimeOffer );
81 return !content.isEmpty();
82}
83
84bool VCardDrag::fromMimeData( const QMimeData *md, KABC::Addressee::List &addressees )
85{
86 const QString mimeOffer = findCompatibleMimeType( md );
87 if ( mimeOffer.isEmpty() ) {
88 return false;
89 }
90 addressees = KABC::VCardConverter().parseVCards( md->data( mimeOffer ) );
91 return !addressees.isEmpty();
92}
93
94// kate: space-indent on; indent-width 2; replace-tabs on;
95