KContacts

vcarddrag.cpp
1 /*
2  This file is part of the KContacts framework.
3 
4  SPDX-FileCopyrightText: 2002 Tobias Koenig <[email protected]>
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 
15 using namespace KContacts;
16 
17 static QString findCompatibleMimeType(const QMimeData *md)
18 {
19  if (!md) {
20  return {};
21  }
22  // check the canonical MIME type first
25  }
26 
27  const QStringList mimeTypeOffers = md->formats();
28  const QMimeDatabase db;
29  for (const QString &mimeType : mimeTypeOffers) {
30  const QMimeType mimeTypePtr = db.mimeTypeForName(mimeType);
31  if (mimeTypePtr.isValid() && mimeTypePtr.inherits(KContacts::Addressee::mimeType())) {
32  return mimeType;
33  }
34  }
35 
36  return QString();
37 }
38 
40 {
42  return true;
43 }
44 
46 {
47  KContacts::VCardConverter converter;
48  const QByteArray vcards = converter.createVCards(addressees);
49  if (!vcards.isEmpty()) {
50  return populateMimeData(md, vcards);
51  } else {
52  return false;
53  }
54 }
55 
57 {
58  return !findCompatibleMimeType(md).isEmpty();
59 }
60 
61 bool 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(mimeOffer);
68  return !content.isEmpty();
69 }
70 
72 {
73  const QString mimeOffer = findCompatibleMimeType(md);
74  if (mimeOffer.isEmpty()) {
75  return false;
76  }
77  addressees = KContacts::VCardConverter().parseVCards(md->data(mimeOffer));
78  return !addressees.isEmpty();
79 }
static QString mimeType()
Returns the MIME type used for Addressees.
Definition: addressee.cpp:2495
QByteArray data(const QString &mimeType) const const
virtual bool hasFormat(const QString &mimeType) const const
bool isEmpty() const const
bool inherits(const QString &mimeTypeName) const const
KCONTACTS_EXPORT bool fromMimeData(const QMimeData *md, QByteArray &content)
Decodes the drag&drop object to vCard component content.
Definition: vcarddrag.cpp:61
Addressee::List parseVCards(const QByteArray &vcard) const
Parses a string in vCard format and returns a list of contact objects.
bool isEmpty() const const
QMimeType mimeTypeForName(const QString &nameOrAlias) const const
KCONTACTS_EXPORT bool canDecode(const QMimeData *md)
Returns if drag&drop object can be decoded to vCard.
Definition: vcarddrag.cpp:56
KCONTACTS_EXPORT bool populateMimeData(QMimeData *md, const QByteArray &content)
Adds the vCard representation as data of the drag object.
Definition: vcarddrag.cpp:39
Class to converting contact objects into vCard format and vice versa.
bool isValid() const const
bool isEmpty() const const
virtual QStringList formats() const const
void setData(const QString &mimeType, const QByteArray &data)
QString mimeType(Type)
QByteArray createVCards(const Addressee::List &list, Version version=v3_0) const
Creates a string in vCard format which contains the given list of contact.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Fri Jun 18 2021 22:54:49 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.