1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the plugins of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:COMM$
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
****************************************************************************/
#include "qxcbmime.h"
#include <QtCore/QTextCodec>
#include <QtGui/QImageWriter>
#include <QtCore/QBuffer>
#include <qdebug.h>
QT_BEGIN_NAMESPACE
QXcbMime::QXcbMime()
: QInternalMimeData()
{ }
QXcbMime::~QXcbMime()
{}
QString QXcbMime::mimeAtomToString(QXcbConnection *connection, xcb_atom_t a)
{
if (a == XCB_NONE)
return QString();
// special cases for string type
if (a == XCB_ATOM_STRING
|| a == connection->atom(QXcbAtom::UTF8_STRING)
|| a == connection->atom(QXcbAtom::TEXT))
return QLatin1String("text/plain");
// special case for images
if (a == XCB_ATOM_PIXMAP)
return QLatin1String("image/ppm");
QByteArray atomName = connection->atomName(a);
// special cases for uris
if (atomName == "text/x-moz-url")
atomName = "text/uri-list";
return QString::fromLatin1(atomName.constData());
}
bool QXcbMime::mimeDataForAtom(QXcbConnection *connection, xcb_atom_t a, QMimeData *mimeData, QByteArray *data,
xcb_atom_t *atomFormat, int *dataFormat)
{
if (!data)
return false;
bool ret = false;
*atomFormat = a;
*dataFormat = 8;
if ((a == connection->atom(QXcbAtom::UTF8_STRING)
|| a == XCB_ATOM_STRING
|| a == connection->atom(QXcbAtom::TEXT))
&& QInternalMimeData::hasFormatHelper(QLatin1String("text/plain"), mimeData)) {
if (a == connection->atom(QXcbAtom::UTF8_STRING)) {
*data = QInternalMimeData::renderDataHelper(QLatin1String("text/plain"), mimeData);
ret = true;
} else if (a == XCB_ATOM_STRING ||
a == connection->atom(QXcbAtom::TEXT)) {
// ICCCM says STRING is latin1
*data = QString::fromUtf8(QInternalMimeData::renderDataHelper(
QLatin1String("text/plain"), mimeData)).toLatin1();
ret = true;
}
return ret;
}
QString atomName = mimeAtomToString(connection, a);
if (QInternalMimeData::hasFormatHelper(atomName, mimeData)) {
*data = QInternalMimeData::renderDataHelper(atomName, mimeData);
// mimeAtomToString() converts "text/x-moz-url" to "text/uri-list",
// so QXcbConnection::atomName() has to be used.
if (atomName == QLatin1String("text/uri-list")
&& connection->atomName(a) == "text/x-moz-url") {
const QString mozUri = QLatin1String(data->split('\n').constFirst()) + QLatin1Char('\n');
*data = QByteArray(reinterpret_cast<const char *>(mozUri.utf16()),
mozUri.length() * 2);
} else if (atomName == QLatin1String("application/x-color"))
*dataFormat = 16;
ret = true;
} else if ((a == XCB_ATOM_PIXMAP || a == XCB_ATOM_BITMAP) && mimeData->hasImage()) {
ret = true;
} else if (atomName == QLatin1String("text/plain")
&& mimeData->hasFormat(QLatin1String("text/uri-list"))) {
// Return URLs also as plain text.
*data = QInternalMimeData::renderDataHelper(atomName, mimeData);
ret = true;
}
return ret;
}
QVector<xcb_atom_t> QXcbMime::mimeAtomsForFormat(QXcbConnection *connection, const QString &format)
{
QVector<xcb_atom_t> atoms;
atoms.reserve(7);
atoms.append(connection->internAtom(format.toLatin1()));
// special cases for strings
if (format == QLatin1String("text/plain")) {
atoms.append(connection->atom(QXcbAtom::UTF8_STRING));
atoms.append(XCB_ATOM_STRING);
atoms.append(connection->atom(QXcbAtom::TEXT));
}
// special cases for uris
if (format == QLatin1String("text/uri-list")) {
atoms.append(connection->internAtom("text/x-moz-url"));
atoms.append(connection->internAtom("text/plain"));
}
//special cases for images
if (format == QLatin1String("image/ppm"))
atoms.append(XCB_ATOM_PIXMAP);
if (format == QLatin1String("image/pbm"))
atoms.append(XCB_ATOM_BITMAP);
return atoms;
}
QVariant QXcbMime::mimeConvertToFormat(QXcbConnection *connection, xcb_atom_t a, const QByteArray &d, const QString &format,
QMetaType::Type requestedType, const QByteArray &encoding)
{
QByteArray data = d;
QString atomName = mimeAtomToString(connection, a);
// qDebug() << "mimeConvertDataToFormat" << format << atomName << data;
if (!encoding.isEmpty()
&& atomName == format + QLatin1String(";charset=") + QLatin1String(encoding)) {
#if QT_CONFIG(textcodec)
if (requestedType == QMetaType::QString) {
QTextCodec *codec = QTextCodec::codecForName(encoding);
if (codec)
return codec->toUnicode(data);
}
#endif
return data;
}
// special cases for string types
if (format == QLatin1String("text/plain")) {
if (data.endsWith('\0'))
data.chop(1);
if (a == connection->atom(QXcbAtom::UTF8_STRING)) {
return QString::fromUtf8(data);
}
if (a == XCB_ATOM_STRING ||
a == connection->atom(QXcbAtom::TEXT))
return QString::fromLatin1(data);
}
// If data contains UTF16 text, convert it to a string.
// Firefox uses UTF16 without BOM for text/x-moz-url, "text/html",
// Google Chrome uses UTF16 without BOM for "text/x-moz-url",
// UTF16 with BOM for "text/html".
if ((format == QLatin1String("text/html") || format == QLatin1String("text/uri-list"))
&& data.size() > 1) {
const quint8 byte0 = data.at(0);
const quint8 byte1 = data.at(1);
if ((byte0 == 0xff && byte1 == 0xfe) || (byte0 == 0xfe && byte1 == 0xff)
|| (byte0 != 0 && byte1 == 0) || (byte0 == 0 && byte1 != 0)) {
const QString str = QString::fromUtf16(
reinterpret_cast<const ushort *>(data.constData()), data.size() / 2);
if (!str.isNull()) {
if (format == QLatin1String("text/uri-list")) {
const auto urls = str.splitRef(QLatin1Char('\n'));
QList<QVariant> list;
list.reserve(urls.size());
for (const QStringRef &s : urls) {
const QUrl url(s.trimmed().toString());
if (url.isValid())
list.append(url);
}
// We expect "text/x-moz-url" as <url><space><title>.
// The atomName variable is not used because mimeAtomToString()
// converts "text/x-moz-url" to "text/uri-list".
if (!list.isEmpty() && connection->atomName(a) == "text/x-moz-url")
return list.constFirst();
return list;
} else {
return str;
}
}
}
// 8 byte encoding, remove a possible 0 at the end
if (data.endsWith('\0'))
data.chop(1);
}
if (atomName == format)
return data;
#if 0 // ###
// special case for images
if (format == QLatin1String("image/ppm")) {
if (a == XCB_ATOM_PIXMAP && data.size() == sizeof(Pixmap)) {
Pixmap xpm = *((Pixmap*)data.data());
if (!xpm)
return QByteArray();
Window root;
int x;
int y;
uint width;
uint height;
uint border_width;
uint depth;
XGetGeometry(display, xpm, &root, &x, &y, &width, &height, &border_width, &depth);
XImage *ximg = XGetImage(display,xpm,x,y,width,height,AllPlanes,depth==1 ? XYPixmap : ZPixmap);
QImage qimg = QXlibStatic::qimageFromXImage(ximg);
XDestroyImage(ximg);
QImageWriter imageWriter;
imageWriter.setFormat("PPMRAW");
QBuffer buf;
buf.open(QIODevice::WriteOnly);
imageWriter.setDevice(&buf);
imageWriter.write(qimg);
return buf.buffer();
}
}
#endif
return QVariant();
}
xcb_atom_t QXcbMime::mimeAtomForFormat(QXcbConnection *connection, const QString &format, QMetaType::Type requestedType,
const QVector<xcb_atom_t> &atoms, QByteArray *requestedEncoding)
{
requestedEncoding->clear();
// find matches for string types
if (format == QLatin1String("text/plain")) {
if (atoms.contains(connection->atom(QXcbAtom::UTF8_STRING)))
return connection->atom(QXcbAtom::UTF8_STRING);
if (atoms.contains(XCB_ATOM_STRING))
return XCB_ATOM_STRING;
if (atoms.contains(connection->atom(QXcbAtom::TEXT)))
return connection->atom(QXcbAtom::TEXT);
}
// find matches for uri types
if (format == QLatin1String("text/uri-list")) {
xcb_atom_t a = connection->internAtom(format.toLatin1());
if (a && atoms.contains(a))
return a;
a = connection->internAtom("text/x-moz-url");
if (a && atoms.contains(a))
return a;
}
// find match for image
if (format == QLatin1String("image/ppm")) {
if (atoms.contains(XCB_ATOM_PIXMAP))
return XCB_ATOM_PIXMAP;
}
// for string/text requests try to use a format with a well-defined charset
// first to avoid encoding problems
if (requestedType == QMetaType::QString
&& format.startsWith(QLatin1String("text/"))
&& !format.contains(QLatin1String("charset="))) {
QString formatWithCharset = format;
formatWithCharset.append(QLatin1String(";charset=utf-8"));
xcb_atom_t a = connection->internAtom(std::move(formatWithCharset).toLatin1());
if (a && atoms.contains(a)) {
*requestedEncoding = "utf-8";
return a;
}
}
xcb_atom_t a = connection->internAtom(format.toLatin1());
if (a && atoms.contains(a))
return a;
return 0;
}
QT_END_NAMESPACE
|