File indexing completed on 2025-01-19 03:39:53
0001 /* 0002 This file is part of the KContacts framework. 0003 0004 SPDX-FileCopyrightText: 2002 Tobias Koenig <tokoe@kde.org> 0005 0006 SPDX-License-Identifier: LGPL-2.0-or-later 0007 */ 0008 0009 #include "vcarddrag.h" 0010 0011 #include "converter/vcardconverter.h" 0012 0013 #include <QMimeDatabase> 0014 0015 using namespace KContacts; 0016 0017 static QString findCompatibleMimeType(const QMimeData *md) 0018 { 0019 if (!md) { 0020 return {}; 0021 } 0022 // check the canonical MIME type first 0023 if (md->hasFormat(KContacts::Addressee::mimeType())) { 0024 return KContacts::Addressee::mimeType(); 0025 } 0026 0027 const QStringList mimeTypeOffers = md->formats(); 0028 const QMimeDatabase db; 0029 for (const QString &mimeType : mimeTypeOffers) { 0030 const QMimeType mimeTypePtr = db.mimeTypeForName(mimeType); 0031 if (mimeTypePtr.isValid() && mimeTypePtr.inherits(KContacts::Addressee::mimeType())) { 0032 return mimeType; 0033 } 0034 } 0035 0036 return QString(); 0037 } 0038 0039 bool VCardDrag::populateMimeData(QMimeData *md, const QByteArray &content) 0040 { 0041 md->setData(KContacts::Addressee::mimeType(), content); 0042 return true; 0043 } 0044 0045 bool VCardDrag::populateMimeData(QMimeData *md, const KContacts::Addressee::List &addressees) 0046 { 0047 KContacts::VCardConverter converter; 0048 const QByteArray vcards = converter.createVCards(addressees); 0049 if (!vcards.isEmpty()) { 0050 return populateMimeData(md, vcards); 0051 } else { 0052 return false; 0053 } 0054 } 0055 0056 bool VCardDrag::canDecode(const QMimeData *md) 0057 { 0058 return !findCompatibleMimeType(md).isEmpty(); 0059 } 0060 0061 bool VCardDrag::fromMimeData(const QMimeData *md, QByteArray &content) 0062 { 0063 const QString mimeOffer = findCompatibleMimeType(md); 0064 if (mimeOffer.isEmpty()) { 0065 return false; 0066 } 0067 content = md->data(mimeOffer); 0068 return !content.isEmpty(); 0069 } 0070 0071 bool VCardDrag::fromMimeData(const QMimeData *md, KContacts::Addressee::List &addressees) 0072 { 0073 const QString mimeOffer = findCompatibleMimeType(md); 0074 if (mimeOffer.isEmpty()) { 0075 return false; 0076 } 0077 addressees = KContacts::VCardConverter().parseVCards(md->data(mimeOffer)); 0078 return !addressees.isEmpty(); 0079 }