KCoreAddons

kurlmimedata.cpp
1 /*
2  This file is part of the KDE libraries
3 
4  SPDX-FileCopyrightText: 2005-2012 David Faure <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8 
9 #include "kurlmimedata.h"
10 #include <QMimeData>
11 #include <QStringList>
12 
13 static QString kdeUriListMime()
14 {
15  return QStringLiteral("application/x-kde4-urilist");
16 } // keep this name "kde4" for compat.
17 
18 static QByteArray uriListData(const QList<QUrl> &urls)
19 {
20  // compatible with qmimedata.cpp encoding of QUrls
21  QByteArray result;
22  for (int i = 0; i < urls.size(); ++i) {
23  result += urls.at(i).toEncoded();
24  result += "\r\n";
25  }
26  return result;
27 }
28 
29 void KUrlMimeData::setUrls(const QList<QUrl> &urls, const QList<QUrl> &mostLocalUrls, QMimeData *mimeData)
30 {
31  // Export the most local urls as text/uri-list and plain text, for non KDE apps.
32  mimeData->setUrls(mostLocalUrls); // set text/uri-list and text/plain
33 
34  // Export the real KIO urls as a kde-specific mimetype
35  mimeData->setData(kdeUriListMime(), uriListData(urls));
36 }
37 
38 void KUrlMimeData::setMetaData(const MetaDataMap &metaData, QMimeData *mimeData)
39 {
40  QByteArray metaDataData; // :)
41  for (MetaDataMap::const_iterator it = metaData.begin(); it != metaData.end(); ++it) {
42  metaDataData += it.key().toUtf8();
43  metaDataData += "[email protected]@$";
44  metaDataData += it.value().toUtf8();
45  metaDataData += "[email protected]@$";
46  }
47  mimeData->setData(QStringLiteral("application/x-kio-metadata"), metaDataData);
48 }
49 
51 {
52  return QStringList{kdeUriListMime(), QStringLiteral("text/uri-list")};
53 }
54 
55 static QList<QUrl> extractKdeUriList(const QMimeData *mimeData)
56 {
57  QList<QUrl> uris;
58  const QByteArray ba = mimeData->data(kdeUriListMime());
59  // Code from qmimedata.cpp
60  QList<QByteArray> urls = ba.split('\n');
61  uris.reserve(urls.size());
62  for (int i = 0; i < urls.size(); ++i) {
63  QByteArray data = urls.at(i).trimmed();
64  if (!data.isEmpty()) {
65  uris.append(QUrl::fromEncoded(data));
66  }
67  }
68  return uris;
69 }
70 
72 {
73  QList<QUrl> uris;
74  if (decodeOptions == PreferLocalUrls) {
75  // Extracting uris from text/uri-list, use the much faster QMimeData method urls()
76  uris = mimeData->urls();
77  if (uris.isEmpty()) {
78  uris = extractKdeUriList(mimeData);
79  }
80  } else {
81  uris = extractKdeUriList(mimeData);
82  if (uris.isEmpty()) {
83  uris = mimeData->urls();
84  }
85  }
86 
87  if (metaData) {
88  const QByteArray metaDataPayload = mimeData->data(QStringLiteral("application/x-kio-metadata"));
89  if (!metaDataPayload.isEmpty()) {
90  QString str = QString::fromUtf8(metaDataPayload.constData());
91  Q_ASSERT(str.endsWith(QLatin1String("[email protected]@$")));
92  str.chop(4);
93  const QStringList lst = str.split(QStringLiteral("[email protected]@$"));
94  bool readingKey = true; // true, then false, then true, etc.
95  QString key;
96  for (QStringList::const_iterator it = lst.begin(); it != lst.end(); ++it) {
97  if (readingKey) {
98  key = *it;
99  } else {
100  metaData->insert(key, *it);
101  }
102  readingKey = !readingKey;
103  }
104  Q_ASSERT(readingKey); // an odd number of items would be, well, odd ;-)
105  }
106  }
107  return uris;
108 }
QByteArray data(const QString &mimeType) const const
QList< QByteArray > split(char sep) const const
KCOREADDONS_EXPORT void setUrls(const QList< QUrl > &urls, const QList< QUrl > &mostLocalUrls, QMimeData *mimeData)
Adds URLs and KIO metadata into the given QMimeData.
void reserve(int alloc)
const T & at(int i) const const
KCOREADDONS_EXPORT QList< QUrl > urlsFromMimeData(const QMimeData *mimeData, DecodeOptions decodeOptions=PreferKdeUrls, MetaDataMap *metaData=nullptr)
Extract a list of urls from the contents of mimeData.
bool isEmpty() const const
void chop(int n)
int size() const const
When the mimedata contains both KDE-style URLs (eg: desktop:/foo) and the "most local" version of the...
Definition: kurlmimedata.h:64
void append(const T &value)
QString fromUtf8(const char *str, int size)
KCOREADDONS_EXPORT void setMetaData(const MetaDataMap &metaData, QMimeData *mimeData)
bool isEmpty() const const
const char * constData() const const
QStringList split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const const
QMap::iterator end()
QMap::iterator begin()
QList::iterator end()
DecodeOptions
Flags to be used in urlsFromMimeData.
Definition: kurlmimedata.h:57
QList< QUrl > urls() const const
KCOREADDONS_EXPORT QStringList mimeDataTypes()
Return the list of mimeTypes that can be decoded by urlsFromMimeData.
QMap::iterator insert(const Key &key, const T &value)
void setData(const QString &mimeType, const QByteArray &data)
QList::iterator begin()
QUrl fromEncoded(const QByteArray &input, QUrl::ParsingMode parsingMode)
void setUrls(const QList< QUrl > &urls)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Wed Apr 21 2021 23:01:58 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.