Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qwindowsdropdataobject.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include <QtCore/qurl.h>
7#include <QtCore/qmimedata.h>
9
11
12using namespace Qt::StringLiterals;
13
14/*!
15 \class QWindowsDropDataObject
16 \brief QWindowsOleDataObject subclass specialized for handling Drag&Drop.
17
18 Prevents "text/uri-list" data for local files from being exported as text
19 or URLs, to allow dropped files to be attached to Office applications
20 (instead of creating local hyperlinks).
21
22 \internal
23*/
24
29
31
33QWindowsDropDataObject::GetData(LPFORMATETC pformatetc, LPSTGMEDIUM pmedium) noexcept
34{
35 if (shouldIgnore(pformatetc))
36 return ResultFromScode(DATA_E_FORMATETC);
37
38 return QWindowsOleDataObject::GetData(pformatetc, pmedium);
39}
40
42QWindowsDropDataObject::QueryGetData(LPFORMATETC pformatetc) noexcept
43{
44 if (shouldIgnore(pformatetc))
45 return ResultFromScode(DATA_E_FORMATETC);
46
47 return QWindowsOleDataObject::QueryGetData(pformatetc);
48}
49
50// If the data is "text/uri-list" only, and all URIs are for local files,
51// we prevent it from being exported as text or URLs, to make target applications
52// like MS Office attach or open the files instead of creating local hyperlinks.
53bool QWindowsDropDataObject::shouldIgnore(LPFORMATETC pformatetc) const
54{
55 QMimeData *dropData = mimeData();
56
57 if (dropData && dropData->formats().size() == 1 && dropData->hasUrls()) {
58 QString formatName = QWindowsMimeRegistry::clipboardFormatName(pformatetc->cfFormat);
59 if (pformatetc->cfFormat == CF_UNICODETEXT
60 || pformatetc->cfFormat == CF_TEXT
61 || formatName == "UniformResourceLocator"_L1
62 || formatName == "UniformResourceLocatorW"_L1) {
63 const auto urls = dropData->urls();
64 return std::all_of(urls.cbegin(), urls.cend(), [] (const QUrl &u) { return u.isLocalFile(); });
65 }
66 }
67
68 return false;
69}
70
71QT_END_NAMESPACE
QWindowsOleDataObject subclass specialized for handling Drag&Drop.
STDMETHOD GetData(LPFORMATETC pformatetcIn, LPSTGMEDIUM pmedium) noexcept override
STDMETHOD QueryGetData(LPFORMATETC pformatetc) noexcept override
QWindowsDropDataObject(QMimeData *mimeData)
~QWindowsDropDataObject() override
OLE data container.
Definition qwindowsole.h:22
QWindowsOleDataObject(QMimeData *mimeData)
QMimeData * mimeData() const