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 <QMimeData>
14 #include <QMimeDatabase>
15 
16 using namespace KContacts;
17 
18 static QString findCompatibleMimeType(const QMimeData *md)
19 {
20  if (!md) {
21  return {};
22  }
23  // check the canonical MIME type first
26  }
27 
28  const QStringList mimeTypeOffers = md->formats();
29  const QMimeDatabase db;
30  for (const QString &mimeType : mimeTypeOffers) {
31  const QMimeType mimeTypePtr = db.mimeTypeForName(mimeType);
32  if (mimeTypePtr.isValid() && mimeTypePtr.inherits(KContacts::Addressee::mimeType())) {
33  return mimeType;
34  }
35  }
36 
37  return QString();
38 }
39 
41 {
43  return true;
44 }
45 
47 {
48  KContacts::VCardConverter converter;
49  const QByteArray vcards = converter.createVCards(addressees);
50  if (!vcards.isEmpty()) {
51  return populateMimeData(md, vcards);
52  } else {
53  return false;
54  }
55 }
56 
58 {
59  return !findCompatibleMimeType(md).isEmpty();
60 }
61 
62 bool VCardDrag::fromMimeData(const QMimeData *md, QByteArray &content)
63 {
64  const QString mimeOffer = findCompatibleMimeType(md);
65  if (mimeOffer.isEmpty()) {
66  return false;
67  }
68  content = md->data(mimeOffer);
69  return !content.isEmpty();
70 }
71 
73 {
74  const QString mimeOffer = findCompatibleMimeType(md);
75  if (mimeOffer.isEmpty()) {
76  return false;
77  }
78  addressees = KContacts::VCardConverter().parseVCards(md->data(mimeOffer));
79  return !addressees.isEmpty();
80 }
static QString mimeType()
Returns the MIME type used for Addressees.
Definition: addressee.cpp:2490
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:62
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:57
KCONTACTS_EXPORT bool populateMimeData(QMimeData *md, const QByteArray &content)
Adds the vCard representation as data of the drag object.
Definition: vcarddrag.cpp:40
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-2020 The KDE developers.
Generated on Fri Aug 7 2020 22:52:57 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.