1/*
2 This file is part of the KContacts framework.
3
4 SPDX-FileCopyrightText: 2002 Tobias Koenig <tokoe@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "vcarddrag.h"
10
11#include "converter/vcardconverter.h"
12
13#include <QMimeDatabase>
14
15using namespace KContacts;
16
17static QString findCompatibleMimeType(const QMimeData *md)
18{
19 if (!md) {
20 return {};
21 }
22 // check the canonical MIME type first
23 if (md->hasFormat(mimetype: KContacts::Addressee::mimeType())) {
24 return KContacts::Addressee::mimeType();
25 }
26
27 const QStringList mimeTypeOffers = md->formats();
28 const QMimeDatabase db;
29 for (const QString &mimeType : mimeTypeOffers) {
30 const QMimeType mimeTypePtr = db.mimeTypeForName(nameOrAlias: mimeType);
31 if (mimeTypePtr.isValid() && mimeTypePtr.inherits(mimeTypeName: KContacts::Addressee::mimeType())) {
32 return mimeType;
33 }
34 }
35
36 return QString();
37}
38
39bool VCardDrag::populateMimeData(QMimeData *md, const QByteArray &content)
40{
41 md->setData(mimetype: KContacts::Addressee::mimeType(), data: content);
42 return true;
43}
44
45bool VCardDrag::populateMimeData(QMimeData *md, const KContacts::Addressee::List &addressees)
46{
47 KContacts::VCardConverter converter;
48 const QByteArray vcards = converter.createVCards(list: addressees);
49 if (!vcards.isEmpty()) {
50 return populateMimeData(md, content: vcards);
51 } else {
52 return false;
53 }
54}
55
56bool VCardDrag::canDecode(const QMimeData *md)
57{
58 return !findCompatibleMimeType(md).isEmpty();
59}
60
61bool VCardDrag::fromMimeData(const QMimeData *md, QByteArray &content)
62{
63 const QString mimeOffer = findCompatibleMimeType(md);
64 if (mimeOffer.isEmpty()) {
65 return false;
66 }
67 content = md->data(mimetype: mimeOffer);
68 return !content.isEmpty();
69}
70
71bool VCardDrag::fromMimeData(const QMimeData *md, KContacts::Addressee::List &addressees)
72{
73 const QString mimeOffer = findCompatibleMimeType(md);
74 if (mimeOffer.isEmpty()) {
75 return false;
76 }
77 addressees = KContacts::VCardConverter().parseVCards(vcard: md->data(mimetype: mimeOffer));
78 return !addressees.isEmpty();
79}
80

source code of kcontacts/src/vcarddrag.cpp