1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the plugins of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include "qwinrtclipboard.h"
41
42 #include <QtCore/QCoreApplication>
43 #include <QtCore/qfunctions_winrt.h>
44 #include <QtCore/private/qeventdispatcher_winrt_p.h>
45
46 #include <Windows.ApplicationModel.datatransfer.h>
47
48 #include <functional>
49
50 using namespace ABI::Windows::ApplicationModel::DataTransfer;
51 using namespace ABI::Windows::Foundation;
52 using namespace Microsoft::WRL;
53 using namespace Microsoft::WRL::Wrappers;
54
55 typedef IEventHandler<IInspectable *> ContentChangedHandler;
56
57 #define RETURN_NULLPTR_IF_FAILED(msg) RETURN_IF_FAILED(msg, return nullptr)
58
59 QT_BEGIN_NAMESPACE
60
QWinRTClipboard()61 QWinRTClipboard::QWinRTClipboard()
62 : m_mimeData(nullptr)
63 {
64 QEventDispatcherWinRT::runOnXamlThread([this]() {
65 HRESULT hr;
66 hr = GetActivationFactory(HString::MakeReference(RuntimeClass_Windows_ApplicationModel_DataTransfer_Clipboard).Get(),
67 &m_nativeClipBoard);
68 Q_ASSERT_SUCCEEDED(hr);
69
70 EventRegistrationToken tok;
71 hr = m_nativeClipBoard->add_ContentChanged(Callback<ContentChangedHandler>(this, &QWinRTClipboard::onContentChanged).Get(), &tok);
72 Q_ASSERT_SUCCEEDED(hr);
73
74 return hr;
75 });
76 }
77
mimeData(QClipboard::Mode mode)78 QMimeData *QWinRTClipboard::mimeData(QClipboard::Mode mode)
79 {
80 if (!supportsMode(mode))
81 return nullptr;
82
83 ComPtr<IDataPackageView> view;
84 HRESULT hr;
85 hr = m_nativeClipBoard->GetContent(&view);
86 RETURN_NULLPTR_IF_FAILED("Could not get clipboard content.");
87
88 ComPtr<IAsyncOperation<HSTRING>> op;
89 HString result;
90 // This throws a security exception (WinRT originate error / 0x40080201.
91 // Unfortunately there seems to be no way to avoid this, neither
92 // running on the XAML thread, nor some other way. Stack Overflow
93 // confirms this problem since Windows (Phone) 8.0.
94 hr = view->GetTextAsync(&op);
95 RETURN_NULLPTR_IF_FAILED("Could not get clipboard text.");
96
97 hr = QWinRTFunctions::await(op, result.GetAddressOf());
98 RETURN_NULLPTR_IF_FAILED("Could not get clipboard text content");
99
100 quint32 size;
101 const wchar_t *textStr = result.GetRawBuffer(&size);
102 QString text = QString::fromWCharArray(textStr, int(size));
103 text.replace(QLatin1String("\r\n"), QLatin1String("\n"));
104
105 if (m_mimeData) {
106 if (m_mimeData->text() == text)
107 return m_mimeData;
108 delete m_mimeData;
109 }
110 m_mimeData = new QMimeData();
111 m_mimeData->setText(text);
112
113 return m_mimeData;
114 }
115
116 // Inspired by QWindowsMimeText::convertFromMime
convertToWindowsLineEnding(const QString & text)117 inline QString convertToWindowsLineEnding(const QString &text)
118 {
119 const QChar *u = text.unicode();
120 QString res;
121 const int s = text.length();
122 int maxsize = s + s / 40 + 3;
123 res.resize(maxsize);
124 int ri = 0;
125 bool cr = false;
126 for (int i = 0; i < s; ++i) {
127 if (*u == QLatin1Char('\r'))
128 cr = true;
129 else {
130 if (*u == QLatin1Char('\n') && !cr)
131 res[ri++] = QLatin1Char('\r');
132 cr = false;
133 }
134 res[ri++] = *u;
135 if (ri+3 >= maxsize) {
136 maxsize += maxsize / 4;
137 res.resize(maxsize);
138 }
139 ++u;
140 }
141 res.truncate(ri);
142 return res;
143 }
144
setMimeData(QMimeData * data,QClipboard::Mode mode)145 void QWinRTClipboard::setMimeData(QMimeData *data, QClipboard::Mode mode)
146 {
147 if (!supportsMode(mode))
148 return;
149
150 const bool newData = !m_mimeData || m_mimeData != data;
151 if (newData) {
152 if (m_mimeData)
153 delete m_mimeData;
154 m_mimeData = data;
155 }
156 const QString text = data ? data->text() : QString();
157 HRESULT hr = QEventDispatcherWinRT::runOnXamlThread([this, text]() {
158 HRESULT hr;
159 ComPtr<IDataPackage> package;
160 hr = RoActivateInstance(HString::MakeReference(RuntimeClass_Windows_ApplicationModel_DataTransfer_DataPackage).Get(),
161 &package);
162
163 const QString nativeString = convertToWindowsLineEnding(text);
164 HStringReference textRef(reinterpret_cast<LPCWSTR>(nativeString.utf16()),
165 uint(nativeString.length()));
166
167 hr = package->SetText(textRef.Get());
168 RETURN_HR_IF_FAILED("Could not set text to clipboard data package.");
169
170 hr = m_nativeClipBoard->SetContent(package.Get());
171 RETURN_HR_IF_FAILED("Could not set clipboard content.");
172 return S_OK;
173 });
174 RETURN_VOID_IF_FAILED("Could not set clipboard text.");
175 }
176
supportsMode(QClipboard::Mode mode) const177 bool QWinRTClipboard::supportsMode(QClipboard::Mode mode) const
178 {
179 return mode == QClipboard::Clipboard;
180 }
181
onContentChanged(IInspectable *,IInspectable *)182 HRESULT QWinRTClipboard::onContentChanged(IInspectable *, IInspectable *)
183 {
184 emitChanged(QClipboard::Clipboard);
185 return S_OK;
186 }
187
188 QT_END_NAMESPACE
189