Debian Codegraph @

src/platform/mac/urlpasteboardmime.mm

raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #include "urlpasteboardmime.h"
4
5 #include "common/mimetypes.h"
6
7 #import <CoreServices/CoreServices.h>
8 #import <Cocoa/Cocoa.h>
9
10 #import <QUrl>
11 #import <QVariant>
12
13 /*
14 * Much of this code is based on the Qt code here:
15 * https://www.qt.gitorious.org/qt/qt/source/src/gui/kernel/qmime_mac.cpp#L765
16 */
17
18 UrlPasteboardMime::UrlPasteboardMime(const QString &urlUti)
19 : UrlPasteboardMimeBase()
20 , m_urlUti(urlUti)
21 {
22 }
23
24 QString UrlPasteboardMime::utiForMime(const QString &mime) const
25 {
26 if (mime.startsWith(QLatin1String(mimeUriList))) {
27 return m_urlUti;
28 }
29 return QString();
30 }
31
32 QString UrlPasteboardMime::mimeForUti(const QString &uti) const
33 {
34 return (uti == m_urlUti) ? mimeUriList : QString();
35 }
36
37 QVariant UrlPasteboardMime::convertToMime(const QString &mime, const QList<QByteArray> &data, const QString &uti) const
38 {
39 if (!canConvert(mime, uti))
40 return QVariant();
41
42 QList<QVariant> ret;
43 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
44 foreach (const QByteArray &urlBytes, data) {
45 QUrl url = QUrl::fromEncoded(urlBytes);
46 if (url.scheme().toLower() == QLatin1String("file")) {
47 // If this is a "file://" URL, we probably got a fileReference URL, which is less
48 // understandable by the user, so we'll change it to a filePath URL instead.
49 NSURL *nsUrl = [NSURL URLWithString: url.toString(QUrl::FullyEncoded).toNSString()];
50 url = QUrl::fromNSURL([nsUrl filePathURL]);
51 }
52 if (url.host().toLower() == QLatin1String("localhost"))
53 url.setHost(QString());
54 url.setPath(url.path().normalized(QString::NormalizationForm_C));
55 ret.append(url);
56 }
57 [pool release];
58
59 return QVariant(ret);
60 }
61
62 QList<QByteArray> UrlPasteboardMime::convertFromMime(const QString &mime, const QVariant &data, const QString &uti) const
63 {
64 QList<QByteArray> ret;
65 if (!canConvert(mime, uti))
66 return ret;
67
68 QVariantList dataList = data.toList();
69 if (dataList.isEmpty()) {
70 // Data has been converted to a newline separated byte array (??)
71 QStringList urls = data.toString().split('\n');
72 foreach (const QUrl &url, QUrl::fromStringList(urls)) {
73 dataList << url;
74 }
75 }
76
77 foreach (const QVariant &urlVariant, dataList) {
78 QUrl url = urlVariant.toUrl();
79 if (url.scheme().isEmpty())
80 url.setScheme(QLatin1String("file"));
81 if (url.scheme().toLower() == QLatin1String("file")) {
82 // If this is a "file://" URL, OS X probably wants a fileReference URL
83 NSURL *nsUrl = [NSURL URLWithString: url.toString(QUrl::FullyEncoded).toNSString()];
84 url = QUrl::fromNSURL([nsUrl fileReferenceURL]);
85 url.setPath(url.path().normalized(QString::NormalizationForm_D));
86 }
87 ret.append(url.toEncoded());
88 }
89
90 return ret;
91 }